
<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>pymatgen.analysis.phase_diagram &#8212; pymatgen 2020.7.3 documentation</title>
    <link rel="stylesheet" href="../../../_static/basic.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
    <script src="../../../_static/jquery.js"></script>
    <script src="../../../_static/underscore.js"></script>
    <script src="../../../_static/doctools.js"></script>
    <script src="../../../_static/language_data.js"></script>
    <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />
 
<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-33990148-1']);
  _gaq.push(['_trackPageview']);
</script>

  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">pymatgen 2020.7.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../../pymatgen.html" accesskey="U">pymatgen</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">pymatgen.analysis.phase_diagram</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
          <div class="body" role="main">
            
  <h1>Source code for pymatgen.analysis.phase_diagram</h1><div class="highlight"><pre>
<span></span><span class="c1"># coding: utf-8</span>
<span class="c1"># Copyright (c) Pymatgen Development Team.</span>
<span class="c1"># Distributed under the terms of the MIT License.</span>

<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">This module defines tools to generate and analyze phase diagrams.</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">import</span> <span class="nn">re</span>
<span class="kn">import</span> <span class="nn">collections</span>
<span class="kn">import</span> <span class="nn">itertools</span>
<span class="kn">import</span> <span class="nn">math</span>
<span class="kn">import</span> <span class="nn">logging</span>

<span class="kn">from</span> <span class="nn">monty.json</span> <span class="kn">import</span> <span class="n">MSONable</span><span class="p">,</span> <span class="n">MontyDecoder</span>
<span class="kn">from</span> <span class="nn">functools</span> <span class="kn">import</span> <span class="n">lru_cache</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">from</span> <span class="nn">scipy.spatial</span> <span class="kn">import</span> <span class="n">ConvexHull</span>

<span class="kn">from</span> <span class="nn">pymatgen.core.composition</span> <span class="kn">import</span> <span class="n">Composition</span>
<span class="kn">from</span> <span class="nn">pymatgen.core.periodic_table</span> <span class="kn">import</span> <span class="n">Element</span><span class="p">,</span> <span class="n">DummySpecie</span><span class="p">,</span> <span class="n">get_el_sp</span>
<span class="kn">from</span> <span class="nn">pymatgen.util.coord</span> <span class="kn">import</span> <span class="n">Simplex</span><span class="p">,</span> <span class="n">in_coord_list</span>
<span class="kn">from</span> <span class="nn">pymatgen.util.string</span> <span class="kn">import</span> <span class="n">latexify</span>
<span class="kn">from</span> <span class="nn">pymatgen.util.plotting</span> <span class="kn">import</span> <span class="n">pretty_plot</span>
<span class="kn">from</span> <span class="nn">pymatgen.analysis.reaction_calculator</span> <span class="kn">import</span> <span class="n">Reaction</span><span class="p">,</span> \
    <span class="n">ReactionError</span>
<span class="kn">from</span> <span class="nn">pymatgen.entries</span> <span class="kn">import</span> <span class="n">Entry</span>

<span class="n">__author__</span> <span class="o">=</span> <span class="s2">&quot;Shyue Ping Ong&quot;</span>
<span class="n">__copyright__</span> <span class="o">=</span> <span class="s2">&quot;Copyright 2011, The Materials Project&quot;</span>
<span class="n">__version__</span> <span class="o">=</span> <span class="s2">&quot;1.0&quot;</span>
<span class="n">__maintainer__</span> <span class="o">=</span> <span class="s2">&quot;Shyue Ping Ong&quot;</span>
<span class="n">__email__</span> <span class="o">=</span> <span class="s2">&quot;shyuep@gmail.com&quot;</span>
<span class="n">__status__</span> <span class="o">=</span> <span class="s2">&quot;Production&quot;</span>
<span class="n">__date__</span> <span class="o">=</span> <span class="s2">&quot;May 16, 2011&quot;</span>

<span class="n">logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="vm">__name__</span><span class="p">)</span>


<div class="viewcode-block" id="PDEntry"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.PDEntry">[docs]</a><span class="k">class</span> <span class="nc">PDEntry</span><span class="p">(</span><span class="n">Entry</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    An object encompassing all relevant data for phase diagrams.</span>

<span class="sd">    .. attribute:: composition</span>

<span class="sd">        The composition associated with the PDEntry.</span>

<span class="sd">    .. attribute:: energy</span>

<span class="sd">        The energy associated with the entry.</span>

<span class="sd">    .. attribute:: name</span>

<span class="sd">        A name for the entry. This is the string shown in the phase diagrams.</span>
<span class="sd">        By default, this is the reduced formula for the composition, but can be</span>
<span class="sd">        set to some other string for display purposes.</span>

<span class="sd">    .. attribute:: attribute</span>

<span class="sd">        A arbitrary attribute. Can be used to specify that the entry is a newly</span>
<span class="sd">        found compound, or to specify a particular label for the entry, etc.</span>
<span class="sd">        An attribute can be anything but must be MSONable.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">composition</span><span class="p">:</span> <span class="n">Composition</span><span class="p">,</span> <span class="n">energy</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span>
                 <span class="n">name</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> <span class="n">attribute</span><span class="p">:</span> <span class="nb">object</span> <span class="o">=</span> <span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Args:</span>
<span class="sd">            composition (Composition): Composition</span>
<span class="sd">            energy (float): Energy for composition.</span>
<span class="sd">            name (str): Optional parameter to name the entry. Defaults </span>
<span class="sd">                to the reduced chemical formula.</span>
<span class="sd">            attribute: Optional attribute of the entry. Must be MSONable.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">composition</span><span class="p">,</span> <span class="n">energy</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">name</span> <span class="k">if</span> <span class="n">name</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">reduced_formula</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">attribute</span> <span class="o">=</span> <span class="n">attribute</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">energy</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">float</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        :return: the energy of the entry.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_energy</span>

    <span class="k">def</span> <span class="fm">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s2">&quot;PDEntry : </span><span class="si">{}</span><span class="s2"> with energy = </span><span class="si">{:.4f}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">composition</span><span class="p">,</span>
                                                          <span class="bp">self</span><span class="o">.</span><span class="n">energy</span><span class="p">)</span>

<div class="viewcode-block" id="PDEntry.as_dict"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.PDEntry.as_dict">[docs]</a>    <span class="k">def</span> <span class="nf">as_dict</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        :return: MSONable dict.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">return_dict</span> <span class="o">=</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">as_dict</span><span class="p">()</span>
        <span class="n">return_dict</span><span class="o">.</span><span class="n">update</span><span class="p">({</span><span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
                            <span class="s2">&quot;attribute&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">attribute</span><span class="p">})</span>
        <span class="k">return</span> <span class="n">return_dict</span></div>

    <span class="k">def</span> <span class="fm">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">as_dict</span><span class="p">()</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">as_dict</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">False</span>

    <span class="k">def</span> <span class="fm">__hash__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">id</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>

<div class="viewcode-block" id="PDEntry.from_dict"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.PDEntry.from_dict">[docs]</a>    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">from_dict</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">d</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        :param d: Dict representation</span>
<span class="sd">        :return: PDEntry</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">cls</span><span class="p">(</span><span class="n">Composition</span><span class="p">(</span><span class="n">d</span><span class="p">[</span><span class="s2">&quot;composition&quot;</span><span class="p">]),</span> <span class="n">d</span><span class="p">[</span><span class="s2">&quot;energy&quot;</span><span class="p">],</span>
                   <span class="n">d</span><span class="p">[</span><span class="s2">&quot;name&quot;</span><span class="p">]</span> <span class="k">if</span> <span class="s2">&quot;name&quot;</span> <span class="ow">in</span> <span class="n">d</span> <span class="k">else</span> <span class="kc">None</span><span class="p">,</span>
                   <span class="n">d</span><span class="p">[</span><span class="s2">&quot;attribute&quot;</span><span class="p">]</span> <span class="k">if</span> <span class="s2">&quot;attribute&quot;</span> <span class="ow">in</span> <span class="n">d</span> <span class="k">else</span> <span class="kc">None</span><span class="p">)</span></div></div>


<div class="viewcode-block" id="GrandPotPDEntry"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.GrandPotPDEntry">[docs]</a><span class="k">class</span> <span class="nc">GrandPotPDEntry</span><span class="p">(</span><span class="n">PDEntry</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A grand potential pd entry object encompassing all relevant data for phase</span>
<span class="sd">    diagrams.  Chemical potentials are given as a element-chemical potential</span>
<span class="sd">    dict.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">entry</span><span class="p">,</span> <span class="n">chempots</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Args:</span>
<span class="sd">            entry: A PDEntry-like object.</span>
<span class="sd">            chempots: Chemical potential specification as {Element: float}.</span>
<span class="sd">            name: Optional parameter to name the entry. Defaults to the reduced</span>
<span class="sd">                chemical formula of the original entry.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">comp</span> <span class="o">=</span> <span class="n">entry</span><span class="o">.</span><span class="n">composition</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">original_entry</span> <span class="o">=</span> <span class="n">entry</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">original_comp</span> <span class="o">=</span> <span class="n">comp</span>
        <span class="n">grandpot</span> <span class="o">=</span> <span class="n">entry</span><span class="o">.</span><span class="n">energy</span> <span class="o">-</span> <span class="nb">sum</span><span class="p">([</span><span class="n">comp</span><span class="p">[</span><span class="n">el</span><span class="p">]</span> <span class="o">*</span> <span class="n">pot</span>
                                       <span class="k">for</span> <span class="n">el</span><span class="p">,</span> <span class="n">pot</span> <span class="ow">in</span> <span class="n">chempots</span><span class="o">.</span><span class="n">items</span><span class="p">()])</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">chempots</span> <span class="o">=</span> <span class="n">chempots</span>
        <span class="n">new_comp_map</span> <span class="o">=</span> <span class="p">{</span><span class="n">el</span><span class="p">:</span> <span class="n">comp</span><span class="p">[</span><span class="n">el</span><span class="p">]</span> <span class="k">for</span> <span class="n">el</span> <span class="ow">in</span> <span class="n">comp</span><span class="o">.</span><span class="n">elements</span>
                        <span class="k">if</span> <span class="n">el</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">chempots</span><span class="p">}</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">new_comp_map</span><span class="p">,</span> <span class="n">grandpot</span><span class="p">,</span> <span class="n">entry</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">name</span> <span class="k">if</span> <span class="n">name</span> <span class="k">else</span> <span class="n">entry</span><span class="o">.</span><span class="n">name</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">is_element</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        True if the entry is an element.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">original_comp</span><span class="o">.</span><span class="n">is_element</span>

    <span class="k">def</span> <span class="fm">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">chempot_str</span> <span class="o">=</span> <span class="s2">&quot; &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="s2">&quot;mu_</span><span class="si">%s</span><span class="s2"> = </span><span class="si">%.4f</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">el</span><span class="p">,</span> <span class="n">mu</span><span class="p">)</span>
                                <span class="k">for</span> <span class="n">el</span><span class="p">,</span> <span class="n">mu</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">chempots</span><span class="o">.</span><span class="n">items</span><span class="p">()])</span>
        <span class="k">return</span> <span class="s2">&quot;GrandPotPDEntry with original composition &quot;</span> <span class="o">+</span> \
               <span class="s2">&quot;</span><span class="si">{}</span><span class="s2">, energy = </span><span class="si">{:.4f}</span><span class="s2">, </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">original_entry</span><span class="o">.</span><span class="n">composition</span><span class="p">,</span>
                                                <span class="bp">self</span><span class="o">.</span><span class="n">original_entry</span><span class="o">.</span><span class="n">energy</span><span class="p">,</span>
                                                <span class="n">chempot_str</span><span class="p">)</span>

    <span class="k">def</span> <span class="fm">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="fm">__repr__</span><span class="p">()</span>

<div class="viewcode-block" id="GrandPotPDEntry.as_dict"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.GrandPotPDEntry.as_dict">[docs]</a>    <span class="k">def</span> <span class="nf">as_dict</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        :return: MSONAble dict</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">{</span><span class="s2">&quot;@module&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__module__</span><span class="p">,</span>
                <span class="s2">&quot;@class&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
                <span class="s2">&quot;entry&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">original_entry</span><span class="o">.</span><span class="n">as_dict</span><span class="p">(),</span>
                <span class="s2">&quot;chempots&quot;</span><span class="p">:</span> <span class="p">{</span><span class="n">el</span><span class="o">.</span><span class="n">symbol</span><span class="p">:</span> <span class="n">u</span> <span class="k">for</span> <span class="n">el</span><span class="p">,</span> <span class="n">u</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">chempots</span><span class="o">.</span><span class="n">items</span><span class="p">()},</span>
                <span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">}</span></div>

<div class="viewcode-block" id="GrandPotPDEntry.from_dict"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.GrandPotPDEntry.from_dict">[docs]</a>    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">from_dict</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">d</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        :param d: Dict representation</span>
<span class="sd">        :return: PDStructureEntry</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">chempots</span> <span class="o">=</span> <span class="p">{</span><span class="n">Element</span><span class="p">(</span><span class="n">symbol</span><span class="p">):</span> <span class="n">u</span> <span class="k">for</span> <span class="n">symbol</span><span class="p">,</span> <span class="n">u</span> <span class="ow">in</span> <span class="n">d</span><span class="p">[</span><span class="s2">&quot;chempots&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">items</span><span class="p">()}</span>
        <span class="n">entry</span> <span class="o">=</span> <span class="n">MontyDecoder</span><span class="p">()</span><span class="o">.</span><span class="n">process_decoded</span><span class="p">(</span><span class="n">d</span><span class="p">[</span><span class="s2">&quot;entry&quot;</span><span class="p">])</span>
        <span class="k">return</span> <span class="bp">cls</span><span class="p">(</span><span class="n">entry</span><span class="p">,</span> <span class="n">chempots</span><span class="p">,</span> <span class="n">d</span><span class="p">[</span><span class="s2">&quot;name&quot;</span><span class="p">])</span></div>

    <span class="k">def</span> <span class="fm">__getattr__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">a</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Delegate attribute to original entry if available.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">original_entry</span><span class="p">,</span> <span class="n">a</span><span class="p">):</span>
            <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">original_entry</span><span class="p">,</span> <span class="n">a</span><span class="p">)</span>
        <span class="k">raise</span> <span class="ne">AttributeError</span><span class="p">(</span><span class="n">a</span><span class="p">)</span></div>


<div class="viewcode-block" id="TransformedPDEntry"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.TransformedPDEntry">[docs]</a><span class="k">class</span> <span class="nc">TransformedPDEntry</span><span class="p">(</span><span class="n">PDEntry</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    This class repesents a TransformedPDEntry, which allows for a PDEntry to be</span>
<span class="sd">    transformed to a different composition coordinate space. It is used in the</span>
<span class="sd">    construction of phase diagrams that do not have elements as the terminal</span>
<span class="sd">    compositions.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">comp</span><span class="p">,</span> <span class="n">original_entry</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Args:</span>
<span class="sd">            comp (Composition): Transformed composition as a Composition.</span>
<span class="sd">            original_entry (PDEntry): Original entry that this entry arose from.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">comp</span><span class="p">,</span> <span class="n">original_entry</span><span class="o">.</span><span class="n">energy</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">original_entry</span> <span class="o">=</span> <span class="n">original_entry</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">original_entry</span><span class="o">.</span><span class="n">name</span>

    <span class="k">def</span> <span class="fm">__getattr__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">a</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Delegate attribute to original entry if available.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">original_entry</span><span class="p">,</span> <span class="n">a</span><span class="p">):</span>
            <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">original_entry</span><span class="p">,</span> <span class="n">a</span><span class="p">)</span>
        <span class="k">raise</span> <span class="ne">AttributeError</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>

    <span class="k">def</span> <span class="fm">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">output</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;TransformedPDEntry </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">composition</span><span class="p">),</span>
                  <span class="s2">&quot; with original composition </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">original_entry</span><span class="o">.</span><span class="n">composition</span><span class="p">),</span>
                  <span class="s2">&quot;, E = </span><span class="si">{:.4f}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">original_entry</span><span class="o">.</span><span class="n">energy</span><span class="p">)]</span>
        <span class="k">return</span> <span class="s2">&quot;&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">output</span><span class="p">)</span>

    <span class="k">def</span> <span class="fm">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="fm">__repr__</span><span class="p">()</span>

<div class="viewcode-block" id="TransformedPDEntry.as_dict"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.TransformedPDEntry.as_dict">[docs]</a>    <span class="k">def</span> <span class="nf">as_dict</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        :return: MSONable dict</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">{</span><span class="s2">&quot;@module&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__module__</span><span class="p">,</span>
                <span class="s2">&quot;@class&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
                <span class="s2">&quot;entry&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">original_entry</span><span class="o">.</span><span class="n">as_dict</span><span class="p">(),</span>
                <span class="s2">&quot;composition&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">composition</span><span class="p">}</span></div>

<div class="viewcode-block" id="TransformedPDEntry.from_dict"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.TransformedPDEntry.from_dict">[docs]</a>    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">from_dict</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">d</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        :param d: Dict representation</span>
<span class="sd">        :return: TransformedPDEntry</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">entry</span> <span class="o">=</span> <span class="n">MontyDecoder</span><span class="p">()</span><span class="o">.</span><span class="n">process_decoded</span><span class="p">(</span><span class="n">d</span><span class="p">[</span><span class="s2">&quot;entry&quot;</span><span class="p">])</span>
        <span class="k">return</span> <span class="bp">cls</span><span class="p">(</span><span class="n">d</span><span class="p">[</span><span class="s2">&quot;composition&quot;</span><span class="p">],</span> <span class="n">entry</span><span class="p">)</span></div></div>


<div class="viewcode-block" id="PhaseDiagram"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.PhaseDiagram">[docs]</a><span class="k">class</span> <span class="nc">PhaseDiagram</span><span class="p">(</span><span class="n">MSONable</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Simple phase diagram class taking in elements and entries as inputs.</span>
<span class="sd">    The algorithm is based on the work in the following papers:</span>

<span class="sd">    1. S. P. Ong, L. Wang, B. Kang, and G. Ceder, Li-Fe-P-O2 Phase Diagram from</span>
<span class="sd">       First Principles Calculations. Chem. Mater., 2008, 20(5), 1798-1807.</span>
<span class="sd">       doi:10.1021/cm702327g</span>

<span class="sd">    2. S. P. Ong, A. Jain, G. Hautier, B. Kang, G. Ceder, Thermal stabilities</span>
<span class="sd">       of delithiated olivine MPO4 (M=Fe, Mn) cathodes investigated using first</span>
<span class="sd">       principles calculations. Electrochem. Comm., 2010, 12(3), 427-430.</span>
<span class="sd">       doi:10.1016/j.elecom.2010.01.010</span>

<span class="sd">    .. attribute: elements:</span>

<span class="sd">        Elements in the phase diagram.</span>

<span class="sd">    ..attribute: all_entries</span>

<span class="sd">        All entries provided for Phase Diagram construction. Note that this</span>
<span class="sd">        does not mean that all these entries are actually used in the phase</span>
<span class="sd">        diagram. For example, this includes the positive formation energy</span>
<span class="sd">        entries that are filtered out before Phase Diagram construction.</span>

<span class="sd">    .. attribute: qhull_data</span>

<span class="sd">        Data used in the convex hull operation. This is essentially a matrix of</span>
<span class="sd">        composition data and energy per atom values created from qhull_entries.</span>

<span class="sd">    .. attribute: qhull_entries:</span>

<span class="sd">        Actual entries used in convex hull. Excludes all positive formation</span>
<span class="sd">        energy entries.</span>

<span class="sd">    .. attribute: dim</span>

<span class="sd">        The dimensionality of the phase diagram.</span>

<span class="sd">    .. attribute: facets</span>

<span class="sd">        Facets of the phase diagram in the form of  [[1,2,3],[4,5,6]...].</span>
<span class="sd">        For a ternary, it is the indices (references to qhull_entries and</span>
<span class="sd">        qhull_data) for the vertices of the phase triangles. Similarly</span>
<span class="sd">        extended to higher D simplices for higher dimensions.</span>

<span class="sd">    .. attribute: el_refs:</span>

<span class="sd">        List of elemental references for the phase diagrams. These are</span>
<span class="sd">        entries corresponding to the lowest energy element entries for simple</span>
<span class="sd">        compositional phase diagrams.</span>

<span class="sd">    .. attribute: simplices:</span>

<span class="sd">        The simplices of the phase diagram as a list of np.ndarray, i.e.,</span>
<span class="sd">        the list of stable compositional coordinates in the phase diagram.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="c1"># Tolerance for determining if formation energy is positive.</span>
    <span class="n">formation_energy_tol</span> <span class="o">=</span> <span class="mf">1e-11</span>
    <span class="n">numerical_tol</span> <span class="o">=</span> <span class="mf">1e-8</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">entries</span><span class="p">,</span> <span class="n">elements</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Standard constructor for phase diagram.</span>

<span class="sd">        Args:</span>
<span class="sd">            entries ([PDEntry]): A list of PDEntry-like objects having an</span>
<span class="sd">                energy, energy_per_atom and composition.</span>
<span class="sd">            elements ([Element]): Optional list of elements in the phase</span>
<span class="sd">                diagram. If set to None, the elements are determined from</span>
<span class="sd">                the the entries themselves and are sorted alphabetically.</span>
<span class="sd">                If specified, element ordering (e.g. for pd coordinates)</span>
<span class="sd">                is preserved.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">elements</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">elements</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
            <span class="k">for</span> <span class="n">entry</span> <span class="ow">in</span> <span class="n">entries</span><span class="p">:</span>
                <span class="n">elements</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">entry</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">elements</span><span class="p">)</span>
            <span class="n">elements</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">elements</span><span class="p">))</span>

        <span class="n">elements</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">elements</span><span class="p">)</span>
        <span class="n">dim</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">elements</span><span class="p">)</span>

        <span class="n">entries</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">entries</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">e</span><span class="p">:</span> <span class="n">e</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">reduced_composition</span><span class="p">)</span>

        <span class="n">el_refs</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="n">min_entries</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">all_entries</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">c</span><span class="p">,</span> <span class="n">g</span> <span class="ow">in</span> <span class="n">itertools</span><span class="o">.</span><span class="n">groupby</span><span class="p">(</span><span class="n">entries</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">e</span><span class="p">:</span> <span class="n">e</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">reduced_composition</span><span class="p">):</span>
            <span class="n">g</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">g</span><span class="p">)</span>
            <span class="n">min_entry</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">g</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">e</span><span class="p">:</span> <span class="n">e</span><span class="o">.</span><span class="n">energy_per_atom</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">c</span><span class="o">.</span><span class="n">is_element</span><span class="p">:</span>
                <span class="n">el_refs</span><span class="p">[</span><span class="n">c</span><span class="o">.</span><span class="n">elements</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span> <span class="o">=</span> <span class="n">min_entry</span>
            <span class="n">min_entries</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">min_entry</span><span class="p">)</span>
            <span class="n">all_entries</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">g</span><span class="p">)</span>

        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">el_refs</span><span class="p">)</span> <span class="o">!=</span> <span class="n">dim</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">PhaseDiagramError</span><span class="p">(</span>
                <span class="s2">&quot;There are no entries associated with a terminal element!.&quot;</span><span class="p">)</span>

        <span class="n">data</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span>
            <span class="p">[</span><span class="n">e</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">get_atomic_fraction</span><span class="p">(</span><span class="n">el</span><span class="p">)</span> <span class="k">for</span> <span class="n">el</span> <span class="ow">in</span> <span class="n">elements</span><span class="p">]</span> <span class="o">+</span> <span class="p">[</span>
                <span class="n">e</span><span class="o">.</span><span class="n">energy_per_atom</span><span class="p">]</span>
            <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">min_entries</span>
        <span class="p">])</span>

        <span class="c1"># Use only entries with negative formation energy</span>
        <span class="n">vec</span> <span class="o">=</span> <span class="p">[</span><span class="n">el_refs</span><span class="p">[</span><span class="n">el</span><span class="p">]</span><span class="o">.</span><span class="n">energy_per_atom</span> <span class="k">for</span> <span class="n">el</span> <span class="ow">in</span> <span class="n">elements</span><span class="p">]</span> <span class="o">+</span> <span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">form_e</span> <span class="o">=</span> <span class="o">-</span><span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">vec</span><span class="p">)</span>
        <span class="n">inds</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">form_e</span> <span class="o">&lt;</span> <span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">formation_energy_tol</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>

        <span class="c1"># Add the elemental references</span>
        <span class="n">inds</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="n">min_entries</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">el</span><span class="p">)</span> <span class="k">for</span> <span class="n">el</span> <span class="ow">in</span> <span class="n">el_refs</span><span class="o">.</span><span class="n">values</span><span class="p">()])</span>

        <span class="n">qhull_entries</span> <span class="o">=</span> <span class="p">[</span><span class="n">min_entries</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">inds</span><span class="p">]</span>
        <span class="n">qhull_data</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">inds</span><span class="p">][:,</span> <span class="mi">1</span><span class="p">:]</span>

        <span class="c1"># Add an extra point to enforce full dimensionality.</span>
        <span class="c1"># This point will be present in all upper hull facets.</span>
        <span class="n">extra_point</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">dim</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span> <span class="o">/</span> <span class="n">dim</span>
        <span class="n">extra_point</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">max</span><span class="p">(</span><span class="n">qhull_data</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span>
        <span class="n">qhull_data</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">concatenate</span><span class="p">([</span><span class="n">qhull_data</span><span class="p">,</span> <span class="p">[</span><span class="n">extra_point</span><span class="p">]],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">dim</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">facets</span> <span class="o">=</span> <span class="p">[</span><span class="n">qhull_data</span><span class="o">.</span><span class="n">argmin</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">facets</span> <span class="o">=</span> <span class="n">get_facets</span><span class="p">(</span><span class="n">qhull_data</span><span class="p">)</span>
            <span class="n">finalfacets</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">facet</span> <span class="ow">in</span> <span class="n">facets</span><span class="p">:</span>
                <span class="c1"># Skip facets that include the extra point</span>
                <span class="k">if</span> <span class="nb">max</span><span class="p">(</span><span class="n">facet</span><span class="p">)</span> <span class="o">==</span> <span class="nb">len</span><span class="p">(</span><span class="n">qhull_data</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">:</span>
                    <span class="k">continue</span>
                <span class="n">m</span> <span class="o">=</span> <span class="n">qhull_data</span><span class="p">[</span><span class="n">facet</span><span class="p">]</span>
                <span class="n">m</span><span class="p">[:,</span> <span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>
                <span class="k">if</span> <span class="nb">abs</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">det</span><span class="p">(</span><span class="n">m</span><span class="p">))</span> <span class="o">&gt;</span> <span class="mf">1e-14</span><span class="p">:</span>
                    <span class="n">finalfacets</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">facet</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">facets</span> <span class="o">=</span> <span class="n">finalfacets</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">simplexes</span> <span class="o">=</span> <span class="p">[</span><span class="n">Simplex</span><span class="p">(</span><span class="n">qhull_data</span><span class="p">[</span><span class="n">f</span><span class="p">,</span> <span class="p">:</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">facets</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">all_entries</span> <span class="o">=</span> <span class="n">all_entries</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">qhull_data</span> <span class="o">=</span> <span class="n">qhull_data</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">dim</span> <span class="o">=</span> <span class="n">dim</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">el_refs</span> <span class="o">=</span> <span class="n">el_refs</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">elements</span> <span class="o">=</span> <span class="n">elements</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">qhull_entries</span> <span class="o">=</span> <span class="n">qhull_entries</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_stable_entries</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">qhull_entries</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span>
                                   <span class="nb">set</span><span class="p">(</span><span class="n">itertools</span><span class="o">.</span><span class="n">chain</span><span class="p">(</span><span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">facets</span><span class="p">)))</span>

<div class="viewcode-block" id="PhaseDiagram.pd_coords"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.PhaseDiagram.pd_coords">[docs]</a>    <span class="k">def</span> <span class="nf">pd_coords</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">comp</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        The phase diagram is generated in a reduced dimensional space</span>
<span class="sd">        (n_elements - 1). This function returns the coordinates in that space.</span>
<span class="sd">        These coordinates are compatible with the stored simplex objects.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="nb">set</span><span class="p">(</span><span class="n">comp</span><span class="o">.</span><span class="n">elements</span><span class="p">)</span><span class="o">.</span><span class="n">difference</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">elements</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">{}</span><span class="s1"> has elements not in the phase diagram </span><span class="si">{}</span><span class="s1">&#39;</span>
                             <span class="s1">&#39;&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">comp</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">elements</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span>
            <span class="p">[</span><span class="n">comp</span><span class="o">.</span><span class="n">get_atomic_fraction</span><span class="p">(</span><span class="n">el</span><span class="p">)</span> <span class="k">for</span> <span class="n">el</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">elements</span><span class="p">[</span><span class="mi">1</span><span class="p">:]])</span></div>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">all_entries_hulldata</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        :return: The actual ndarray used to construct the convex hull.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">data</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">entry</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">all_entries</span><span class="p">:</span>
            <span class="n">comp</span> <span class="o">=</span> <span class="n">entry</span><span class="o">.</span><span class="n">composition</span>
            <span class="n">row</span> <span class="o">=</span> <span class="p">[</span><span class="n">comp</span><span class="o">.</span><span class="n">get_atomic_fraction</span><span class="p">(</span><span class="n">el</span><span class="p">)</span> <span class="k">for</span> <span class="n">el</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">elements</span><span class="p">]</span>
            <span class="n">row</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">entry</span><span class="o">.</span><span class="n">energy_per_atom</span><span class="p">)</span>
            <span class="n">data</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">row</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">data</span><span class="p">)[:,</span> <span class="mi">1</span><span class="p">:]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">unstable_entries</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Entries that are unstable in the phase diagram. Includes positive</span>
<span class="sd">        formation energy entries.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">[</span><span class="n">e</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">all_entries</span> <span class="k">if</span> <span class="n">e</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">stable_entries</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">stable_entries</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the stable entries in the phase diagram.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_stable_entries</span>

<div class="viewcode-block" id="PhaseDiagram.get_form_energy"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.PhaseDiagram.get_form_energy">[docs]</a>    <span class="k">def</span> <span class="nf">get_form_energy</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">entry</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the formation energy for an entry (NOT normalized) from the</span>
<span class="sd">        elemental references.</span>

<span class="sd">        Args:</span>
<span class="sd">            entry: A PDEntry-like object.</span>

<span class="sd">        Returns:</span>
<span class="sd">            Formation energy from the elemental references.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">c</span> <span class="o">=</span> <span class="n">entry</span><span class="o">.</span><span class="n">composition</span>
        <span class="k">return</span> <span class="n">entry</span><span class="o">.</span><span class="n">energy</span> <span class="o">-</span> <span class="nb">sum</span><span class="p">([</span><span class="n">c</span><span class="p">[</span><span class="n">el</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">el_refs</span><span class="p">[</span><span class="n">el</span><span class="p">]</span><span class="o">.</span><span class="n">energy_per_atom</span>
                                   <span class="k">for</span> <span class="n">el</span> <span class="ow">in</span> <span class="n">c</span><span class="o">.</span><span class="n">elements</span><span class="p">])</span></div>

<div class="viewcode-block" id="PhaseDiagram.get_form_energy_per_atom"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.PhaseDiagram.get_form_energy_per_atom">[docs]</a>    <span class="k">def</span> <span class="nf">get_form_energy_per_atom</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">entry</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the formation energy per atom for an entry from the</span>
<span class="sd">        elemental references.</span>

<span class="sd">        Args:</span>
<span class="sd">            entry: An PDEntry-like object</span>

<span class="sd">        Returns:</span>
<span class="sd">            Formation energy **per atom** from the elemental references.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_form_energy</span><span class="p">(</span><span class="n">entry</span><span class="p">)</span> <span class="o">/</span> <span class="n">entry</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">num_atoms</span></div>

    <span class="k">def</span> <span class="fm">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="fm">__str__</span><span class="p">()</span>

    <span class="k">def</span> <span class="fm">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">symbols</span> <span class="o">=</span> <span class="p">[</span><span class="n">el</span><span class="o">.</span><span class="n">symbol</span> <span class="k">for</span> <span class="n">el</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">elements</span><span class="p">]</span>
        <span class="n">output</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;</span><span class="si">{}</span><span class="s2"> phase diagram&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s2">&quot;-&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">symbols</span><span class="p">)),</span>
                  <span class="s2">&quot;</span><span class="si">{}</span><span class="s2"> stable phases: &quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">stable_entries</span><span class="p">)),</span>
                  <span class="s2">&quot;, &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="n">entry</span><span class="o">.</span><span class="n">name</span>
                             <span class="k">for</span> <span class="n">entry</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">stable_entries</span><span class="p">])]</span>
        <span class="k">return</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">output</span><span class="p">)</span>

<div class="viewcode-block" id="PhaseDiagram.as_dict"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.PhaseDiagram.as_dict">[docs]</a>    <span class="k">def</span> <span class="nf">as_dict</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        :return: MSONAble dict</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">{</span><span class="s2">&quot;@module&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__module__</span><span class="p">,</span>
                <span class="s2">&quot;@class&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
                <span class="s2">&quot;all_entries&quot;</span><span class="p">:</span> <span class="p">[</span><span class="n">e</span><span class="o">.</span><span class="n">as_dict</span><span class="p">()</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">all_entries</span><span class="p">],</span>
                <span class="s2">&quot;elements&quot;</span><span class="p">:</span> <span class="p">[</span><span class="n">e</span><span class="o">.</span><span class="n">as_dict</span><span class="p">()</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">elements</span><span class="p">]}</span></div>

<div class="viewcode-block" id="PhaseDiagram.from_dict"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.PhaseDiagram.from_dict">[docs]</a>    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">from_dict</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">d</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        :param d: Dict representation</span>
<span class="sd">        :return: PhaseDiagram</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">entries</span> <span class="o">=</span> <span class="p">[</span><span class="n">MontyDecoder</span><span class="p">()</span><span class="o">.</span><span class="n">process_decoded</span><span class="p">(</span><span class="n">dd</span><span class="p">)</span> <span class="k">for</span> <span class="n">dd</span> <span class="ow">in</span> <span class="n">d</span><span class="p">[</span><span class="s2">&quot;all_entries&quot;</span><span class="p">]]</span>
        <span class="n">elements</span> <span class="o">=</span> <span class="p">[</span><span class="n">Element</span><span class="o">.</span><span class="n">from_dict</span><span class="p">(</span><span class="n">dd</span><span class="p">)</span> <span class="k">for</span> <span class="n">dd</span> <span class="ow">in</span> <span class="n">d</span><span class="p">[</span><span class="s2">&quot;elements&quot;</span><span class="p">]]</span>
        <span class="k">return</span> <span class="bp">cls</span><span class="p">(</span><span class="n">entries</span><span class="p">,</span> <span class="n">elements</span><span class="p">)</span></div>

    <span class="nd">@lru_cache</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">_get_facet_and_simplex</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">comp</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get any facet that a composition falls into. Cached so successive</span>
<span class="sd">        calls at same composition are fast.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">c</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">pd_coords</span><span class="p">(</span><span class="n">comp</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">f</span><span class="p">,</span> <span class="n">s</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">facets</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">simplexes</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">s</span><span class="o">.</span><span class="n">in_simplex</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">PhaseDiagram</span><span class="o">.</span><span class="n">numerical_tol</span> <span class="o">/</span> <span class="mi">10</span><span class="p">):</span>
                <span class="k">return</span> <span class="n">f</span><span class="p">,</span> <span class="n">s</span>
        <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">&quot;No facet found for comp = </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">comp</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">_get_facet_chempots</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">facet</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Calculates the chemical potentials for each element within a facet.</span>

<span class="sd">        Args:</span>
<span class="sd">            facet: Facet of the phase diagram.</span>

<span class="sd">        Returns:</span>
<span class="sd">            { element: chempot } for all elements in the phase diagram.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">complist</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">qhull_entries</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">composition</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">facet</span><span class="p">]</span>
        <span class="n">energylist</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">qhull_entries</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">energy_per_atom</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">facet</span><span class="p">]</span>
        <span class="n">m</span> <span class="o">=</span> <span class="p">[[</span><span class="n">c</span><span class="o">.</span><span class="n">get_atomic_fraction</span><span class="p">(</span><span class="n">e</span><span class="p">)</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">elements</span><span class="p">]</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span>
             <span class="n">complist</span><span class="p">]</span>
        <span class="n">chempots</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">solve</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">energylist</span><span class="p">)</span>
        <span class="k">return</span> <span class="nb">dict</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">elements</span><span class="p">,</span> <span class="n">chempots</span><span class="p">))</span>

<div class="viewcode-block" id="PhaseDiagram.get_decomposition"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.PhaseDiagram.get_decomposition">[docs]</a>    <span class="k">def</span> <span class="nf">get_decomposition</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">comp</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Provides the decomposition at a particular composition.</span>

<span class="sd">        Args:</span>
<span class="sd">            comp: A composition</span>

<span class="sd">        Returns:</span>
<span class="sd">            Decomposition as a dict of {Entry: amount}</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">facet</span><span class="p">,</span> <span class="n">simplex</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_facet_and_simplex</span><span class="p">(</span><span class="n">comp</span><span class="p">)</span>
        <span class="n">decomp_amts</span> <span class="o">=</span> <span class="n">simplex</span><span class="o">.</span><span class="n">bary_coords</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">pd_coords</span><span class="p">(</span><span class="n">comp</span><span class="p">))</span>
        <span class="k">return</span> <span class="p">{</span><span class="bp">self</span><span class="o">.</span><span class="n">qhull_entries</span><span class="p">[</span><span class="n">f</span><span class="p">]:</span> <span class="n">amt</span>
                <span class="k">for</span> <span class="n">f</span><span class="p">,</span> <span class="n">amt</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">facet</span><span class="p">,</span> <span class="n">decomp_amts</span><span class="p">)</span>
                <span class="k">if</span> <span class="nb">abs</span><span class="p">(</span><span class="n">amt</span><span class="p">)</span> <span class="o">&gt;</span> <span class="n">PhaseDiagram</span><span class="o">.</span><span class="n">numerical_tol</span><span class="p">}</span></div>

<div class="viewcode-block" id="PhaseDiagram.get_hull_energy"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.PhaseDiagram.get_hull_energy">[docs]</a>    <span class="k">def</span> <span class="nf">get_hull_energy</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">comp</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Args:</span>
<span class="sd">            comp (Composition): Input composition</span>

<span class="sd">        Returns:</span>
<span class="sd">            Energy of lowest energy equilibrium at desired composition. Not</span>
<span class="sd">            normalized by atoms, i.e. E(Li4O2) = 2 * E(Li2O)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">e</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_decomposition</span><span class="p">(</span><span class="n">comp</span><span class="p">)</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="n">e</span> <span class="o">+=</span> <span class="n">k</span><span class="o">.</span><span class="n">energy_per_atom</span> <span class="o">*</span> <span class="n">v</span>
        <span class="k">return</span> <span class="n">e</span> <span class="o">*</span> <span class="n">comp</span><span class="o">.</span><span class="n">num_atoms</span></div>

<div class="viewcode-block" id="PhaseDiagram.get_decomp_and_e_above_hull"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.PhaseDiagram.get_decomp_and_e_above_hull">[docs]</a>    <span class="k">def</span> <span class="nf">get_decomp_and_e_above_hull</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">entry</span><span class="p">,</span> <span class="n">allow_negative</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Provides the decomposition and energy above convex hull for an entry.</span>
<span class="sd">        Due to caching, can be much faster if entries with the same composition</span>
<span class="sd">        are processed together.</span>

<span class="sd">        Args:</span>
<span class="sd">            entry: A PDEntry like object</span>
<span class="sd">            allow_negative: Whether to allow negative e_above_hulls. Used to</span>
<span class="sd">                calculate equilibrium reaction energies. Defaults to False.</span>

<span class="sd">        Returns:</span>
<span class="sd">            (decomp, energy above convex hull)  Stable entries should have</span>
<span class="sd">            energy above hull of 0. The decomposition is provided as a dict of</span>
<span class="sd">            {Entry: amount}.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">entry</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">stable_entries</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">{</span><span class="n">entry</span><span class="p">:</span> <span class="mi">1</span><span class="p">},</span> <span class="mi">0</span>

        <span class="n">comp</span> <span class="o">=</span> <span class="n">entry</span><span class="o">.</span><span class="n">composition</span>
        <span class="n">facet</span><span class="p">,</span> <span class="n">simplex</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_facet_and_simplex</span><span class="p">(</span><span class="n">comp</span><span class="p">)</span>
        <span class="n">decomp_amts</span> <span class="o">=</span> <span class="n">simplex</span><span class="o">.</span><span class="n">bary_coords</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">pd_coords</span><span class="p">(</span><span class="n">comp</span><span class="p">))</span>
        <span class="n">decomp</span> <span class="o">=</span> <span class="p">{</span><span class="bp">self</span><span class="o">.</span><span class="n">qhull_entries</span><span class="p">[</span><span class="n">f</span><span class="p">]:</span> <span class="n">amt</span>
                  <span class="k">for</span> <span class="n">f</span><span class="p">,</span> <span class="n">amt</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">facet</span><span class="p">,</span> <span class="n">decomp_amts</span><span class="p">)</span>
                  <span class="k">if</span> <span class="nb">abs</span><span class="p">(</span><span class="n">amt</span><span class="p">)</span> <span class="o">&gt;</span> <span class="n">PhaseDiagram</span><span class="o">.</span><span class="n">numerical_tol</span><span class="p">}</span>
        <span class="n">energies</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">qhull_entries</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">energy_per_atom</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">facet</span><span class="p">]</span>
        <span class="n">ehull</span> <span class="o">=</span> <span class="n">entry</span><span class="o">.</span><span class="n">energy_per_atom</span> <span class="o">-</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">decomp_amts</span><span class="p">,</span> <span class="n">energies</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">allow_negative</span> <span class="ow">or</span> <span class="n">ehull</span> <span class="o">&gt;=</span> <span class="o">-</span><span class="n">PhaseDiagram</span><span class="o">.</span><span class="n">numerical_tol</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">decomp</span><span class="p">,</span> <span class="n">ehull</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;No valid decomp found!&quot;</span><span class="p">)</span></div>

<div class="viewcode-block" id="PhaseDiagram.get_e_above_hull"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.PhaseDiagram.get_e_above_hull">[docs]</a>    <span class="k">def</span> <span class="nf">get_e_above_hull</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">entry</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Provides the energy above convex hull for an entry</span>

<span class="sd">        Args:</span>
<span class="sd">            entry: A PDEntry like object</span>

<span class="sd">        Returns:</span>
<span class="sd">            Energy above convex hull of entry. Stable entries should have</span>
<span class="sd">            energy above hull of 0.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_decomp_and_e_above_hull</span><span class="p">(</span><span class="n">entry</span><span class="p">)[</span><span class="mi">1</span><span class="p">]</span></div>

<div class="viewcode-block" id="PhaseDiagram.get_equilibrium_reaction_energy"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.PhaseDiagram.get_equilibrium_reaction_energy">[docs]</a>    <span class="k">def</span> <span class="nf">get_equilibrium_reaction_energy</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">entry</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Provides the reaction energy of a stable entry from the neighboring</span>
<span class="sd">        equilibrium stable entries (also known as the inverse distance to</span>
<span class="sd">        hull).</span>

<span class="sd">        Args:</span>
<span class="sd">            entry: A PDEntry like object</span>

<span class="sd">        Returns:</span>
<span class="sd">            Equilibrium reaction energy of entry. Stable entries should have</span>
<span class="sd">            equilibrium reaction energy &lt;= 0.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">entry</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">stable_entries</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Equilibrium reaction energy is available only &quot;</span>
                             <span class="s2">&quot;for stable entries.&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">entry</span><span class="o">.</span><span class="n">is_element</span><span class="p">:</span>
            <span class="k">return</span> <span class="mi">0</span>
        <span class="n">entries</span> <span class="o">=</span> <span class="p">[</span><span class="n">e</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">stable_entries</span> <span class="k">if</span> <span class="n">e</span> <span class="o">!=</span> <span class="n">entry</span><span class="p">]</span>
        <span class="n">modpd</span> <span class="o">=</span> <span class="n">PhaseDiagram</span><span class="p">(</span><span class="n">entries</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">elements</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">modpd</span><span class="o">.</span><span class="n">get_decomp_and_e_above_hull</span><span class="p">(</span><span class="n">entry</span><span class="p">,</span>
                                                 <span class="n">allow_negative</span><span class="o">=</span><span class="kc">True</span><span class="p">)[</span><span class="mi">1</span><span class="p">]</span></div>

<div class="viewcode-block" id="PhaseDiagram.get_composition_chempots"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.PhaseDiagram.get_composition_chempots">[docs]</a>    <span class="k">def</span> <span class="nf">get_composition_chempots</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">comp</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get the chemical potentials for all elements at a given composition.</span>

<span class="sd">        :param comp: Composition</span>
<span class="sd">        :return: Dict of chemical potentials.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">facet</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_facet_and_simplex</span><span class="p">(</span><span class="n">comp</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_facet_chempots</span><span class="p">(</span><span class="n">facet</span><span class="p">)</span></div>

<div class="viewcode-block" id="PhaseDiagram.get_all_chempots"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.PhaseDiagram.get_all_chempots">[docs]</a>    <span class="k">def</span> <span class="nf">get_all_chempots</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">comp</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get chemical potentials at a given compositon.</span>

<span class="sd">        :param comp: Composition</span>
<span class="sd">        :return: Chemical potentials.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># note the top part takes from format of _get_facet_and_simplex,</span>
        <span class="c1">#   but wants to return all facets rather than the first one that meets this criteria</span>
        <span class="n">c</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">pd_coords</span><span class="p">(</span><span class="n">comp</span><span class="p">)</span>
        <span class="n">allfacets</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">f</span><span class="p">,</span> <span class="n">s</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">facets</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">simplexes</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">s</span><span class="o">.</span><span class="n">in_simplex</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">PhaseDiagram</span><span class="o">.</span><span class="n">numerical_tol</span> <span class="o">/</span> <span class="mi">10</span><span class="p">):</span>
                <span class="n">allfacets</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="nb">len</span><span class="p">(</span><span class="n">allfacets</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">&quot;No facets found for comp = </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">comp</span><span class="p">))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">chempots</span> <span class="o">=</span> <span class="p">{}</span>
            <span class="k">for</span> <span class="n">facet</span> <span class="ow">in</span> <span class="n">allfacets</span><span class="p">:</span>
                <span class="n">facet_elt_list</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">qhull_entries</span><span class="p">[</span><span class="n">j</span><span class="p">]</span><span class="o">.</span><span class="n">name</span> <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="n">facet</span><span class="p">]</span>
                <span class="n">facet_name</span> <span class="o">=</span> <span class="s1">&#39;-&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">facet_elt_list</span><span class="p">)</span>
                <span class="n">chempots</span><span class="p">[</span><span class="n">facet_name</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_facet_chempots</span><span class="p">(</span><span class="n">facet</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">chempots</span></div>

<div class="viewcode-block" id="PhaseDiagram.get_transition_chempots"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.PhaseDiagram.get_transition_chempots">[docs]</a>    <span class="k">def</span> <span class="nf">get_transition_chempots</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">element</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get the critical chemical potentials for an element in the Phase</span>
<span class="sd">        Diagram.</span>

<span class="sd">        Args:</span>
<span class="sd">            element: An element. Has to be in the PD in the first place.</span>

<span class="sd">        Returns:</span>
<span class="sd">            A sorted sequence of critical chemical potentials, from less</span>
<span class="sd">            negative to more negative.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">element</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">elements</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;get_transition_chempots can only be called with &quot;</span>
                             <span class="s2">&quot;elements in the phase diagram.&quot;</span><span class="p">)</span>

        <span class="n">critical_chempots</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">facet</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">facets</span><span class="p">:</span>
            <span class="n">chempots</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_facet_chempots</span><span class="p">(</span><span class="n">facet</span><span class="p">)</span>
            <span class="n">critical_chempots</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">chempots</span><span class="p">[</span><span class="n">element</span><span class="p">])</span>

        <span class="n">clean_pots</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">critical_chempots</span><span class="p">):</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">clean_pots</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">clean_pots</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">c</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">if</span> <span class="nb">abs</span><span class="p">(</span><span class="n">c</span> <span class="o">-</span> <span class="n">clean_pots</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span> <span class="o">&gt;</span> <span class="n">PhaseDiagram</span><span class="o">.</span><span class="n">numerical_tol</span><span class="p">:</span>
                    <span class="n">clean_pots</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">c</span><span class="p">)</span>
        <span class="n">clean_pots</span><span class="o">.</span><span class="n">reverse</span><span class="p">()</span>
        <span class="k">return</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">clean_pots</span><span class="p">)</span></div>

<div class="viewcode-block" id="PhaseDiagram.get_critical_compositions"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.PhaseDiagram.get_critical_compositions">[docs]</a>    <span class="k">def</span> <span class="nf">get_critical_compositions</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">comp1</span><span class="p">,</span> <span class="n">comp2</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get the critical compositions along the tieline between two</span>
<span class="sd">        compositions. I.e. where the decomposition products change.</span>
<span class="sd">        The endpoints are also returned.</span>
<span class="sd">        Args:</span>
<span class="sd">            comp1, comp2 (Composition): compositions that define the tieline</span>
<span class="sd">        Returns:</span>
<span class="sd">            [(Composition)]: list of critical compositions. All are of</span>
<span class="sd">                the form x * comp1 + (1-x) * comp2</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">n1</span> <span class="o">=</span> <span class="n">comp1</span><span class="o">.</span><span class="n">num_atoms</span>
        <span class="n">n2</span> <span class="o">=</span> <span class="n">comp2</span><span class="o">.</span><span class="n">num_atoms</span>
        <span class="n">pd_els</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">elements</span>

        <span class="c1"># the reduced dimensionality Simplexes don&#39;t use the</span>
        <span class="c1"># first element in the PD</span>
        <span class="n">c1</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">pd_coords</span><span class="p">(</span><span class="n">comp1</span><span class="p">)</span>
        <span class="n">c2</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">pd_coords</span><span class="p">(</span><span class="n">comp2</span><span class="p">)</span>

        <span class="c1"># none of the projections work if c1 == c2, so just return *copies*</span>
        <span class="c1"># of the inputs</span>
        <span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">all</span><span class="p">(</span><span class="n">c1</span> <span class="o">==</span> <span class="n">c2</span><span class="p">):</span>
            <span class="k">return</span> <span class="p">[</span><span class="n">comp1</span><span class="o">.</span><span class="n">copy</span><span class="p">(),</span> <span class="n">comp2</span><span class="o">.</span><span class="n">copy</span><span class="p">()]</span>

        <span class="n">intersections</span> <span class="o">=</span> <span class="p">[</span><span class="n">c1</span><span class="p">,</span> <span class="n">c2</span><span class="p">]</span>
        <span class="k">for</span> <span class="n">sc</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">simplexes</span><span class="p">:</span>
            <span class="n">intersections</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">sc</span><span class="o">.</span><span class="n">line_intersection</span><span class="p">(</span><span class="n">c1</span><span class="p">,</span> <span class="n">c2</span><span class="p">))</span>
        <span class="n">intersections</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">intersections</span><span class="p">)</span>

        <span class="c1"># find position along line</span>
        <span class="n">l</span> <span class="o">=</span> <span class="p">(</span><span class="n">c2</span> <span class="o">-</span> <span class="n">c1</span><span class="p">)</span>
        <span class="n">l</span> <span class="o">/=</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">l</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span> <span class="o">**</span> <span class="mf">0.5</span>
        <span class="n">proj</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">intersections</span> <span class="o">-</span> <span class="n">c1</span><span class="p">,</span> <span class="n">l</span><span class="p">)</span>

        <span class="c1"># only take compositions between endpoints</span>
        <span class="n">proj</span> <span class="o">=</span> <span class="n">proj</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">logical_and</span><span class="p">(</span><span class="n">proj</span> <span class="o">&gt;</span> <span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">numerical_tol</span><span class="p">,</span>
                                   <span class="n">proj</span> <span class="o">&lt;</span> <span class="n">proj</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">numerical_tol</span><span class="p">)]</span>
        <span class="n">proj</span><span class="o">.</span><span class="n">sort</span><span class="p">()</span>

        <span class="c1"># only unique compositions</span>
        <span class="n">valid</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">proj</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">bool</span><span class="p">)</span>
        <span class="n">valid</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span> <span class="o">=</span> <span class="n">proj</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span> <span class="o">&gt;</span> <span class="n">proj</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">numerical_tol</span>
        <span class="n">proj</span> <span class="o">=</span> <span class="n">proj</span><span class="p">[</span><span class="n">valid</span><span class="p">]</span>

        <span class="n">ints</span> <span class="o">=</span> <span class="n">c1</span> <span class="o">+</span> <span class="n">l</span> <span class="o">*</span> <span class="n">proj</span><span class="p">[:,</span> <span class="kc">None</span><span class="p">]</span>
        <span class="c1"># reconstruct full-dimensional composition array</span>
        <span class="n">cs</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">concatenate</span><span class="p">([</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span> <span class="o">-</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">ints</span><span class="p">,</span> <span class="n">axis</span><span class="o">=-</span><span class="mi">1</span><span class="p">)])</span><span class="o">.</span><span class="n">T</span><span class="p">,</span>
                             <span class="n">ints</span><span class="p">],</span> <span class="n">axis</span><span class="o">=-</span><span class="mi">1</span><span class="p">)</span>
        <span class="c1"># mixing fraction when compositions are normalized</span>
        <span class="n">x</span> <span class="o">=</span> <span class="n">proj</span> <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">c2</span> <span class="o">-</span> <span class="n">c1</span><span class="p">,</span> <span class="n">l</span><span class="p">)</span>
        <span class="c1"># mixing fraction when compositions are not normalized</span>
        <span class="n">x_unnormalized</span> <span class="o">=</span> <span class="n">x</span> <span class="o">*</span> <span class="n">n1</span> <span class="o">/</span> <span class="p">(</span><span class="n">n2</span> <span class="o">+</span> <span class="n">x</span> <span class="o">*</span> <span class="p">(</span><span class="n">n1</span> <span class="o">-</span> <span class="n">n2</span><span class="p">))</span>
        <span class="n">num_atoms</span> <span class="o">=</span> <span class="n">n1</span> <span class="o">+</span> <span class="p">(</span><span class="n">n2</span> <span class="o">-</span> <span class="n">n1</span><span class="p">)</span> <span class="o">*</span> <span class="n">x_unnormalized</span>
        <span class="n">cs</span> <span class="o">*=</span> <span class="n">num_atoms</span><span class="p">[:,</span> <span class="kc">None</span><span class="p">]</span>
        <span class="k">return</span> <span class="p">[</span><span class="n">Composition</span><span class="p">((</span><span class="n">c</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span> <span class="k">for</span> <span class="n">c</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">pd_els</span><span class="p">,</span> <span class="n">m</span><span class="p">))</span> <span class="k">for</span> <span class="n">m</span> <span class="ow">in</span> <span class="n">cs</span><span class="p">]</span></div>

<div class="viewcode-block" id="PhaseDiagram.get_element_profile"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.PhaseDiagram.get_element_profile">[docs]</a>    <span class="k">def</span> <span class="nf">get_element_profile</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">element</span><span class="p">,</span> <span class="n">comp</span><span class="p">,</span> <span class="n">comp_tol</span><span class="o">=</span><span class="mf">1e-5</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Provides the element evolution data for a composition.</span>
<span class="sd">        For example, can be used to analyze Li conversion voltages by varying</span>
<span class="sd">        uLi and looking at the phases formed. Also can be used to analyze O2</span>
<span class="sd">        evolution by varying uO2.</span>

<span class="sd">        Args:</span>
<span class="sd">            element: An element. Must be in the phase diagram.</span>
<span class="sd">            comp: A Composition</span>
<span class="sd">            comp_tol: The tolerance to use when calculating decompositions.</span>
<span class="sd">                Phases with amounts less than this tolerance are excluded.</span>
<span class="sd">                Defaults to 1e-5.</span>

<span class="sd">        Returns:</span>
<span class="sd">            Evolution data as a list of dictionaries of the following format:</span>
<span class="sd">            [ {&#39;chempot&#39;: -10.487582010000001, &#39;evolution&#39;: -2.0,</span>
<span class="sd">            &#39;reaction&#39;: Reaction Object], ...]</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">element</span> <span class="o">=</span> <span class="n">get_el_sp</span><span class="p">(</span><span class="n">element</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">element</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">elements</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;get_transition_chempots can only be called with&quot;</span>
                             <span class="s2">&quot; elements in the phase diagram.&quot;</span><span class="p">)</span>
        <span class="n">gccomp</span> <span class="o">=</span> <span class="n">Composition</span><span class="p">({</span><span class="n">el</span><span class="p">:</span> <span class="n">amt</span> <span class="k">for</span> <span class="n">el</span><span class="p">,</span> <span class="n">amt</span> <span class="ow">in</span> <span class="n">comp</span><span class="o">.</span><span class="n">items</span><span class="p">()</span>
                              <span class="k">if</span> <span class="n">el</span> <span class="o">!=</span> <span class="n">element</span><span class="p">})</span>
        <span class="n">elref</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">el_refs</span><span class="p">[</span><span class="n">element</span><span class="p">]</span>
        <span class="n">elcomp</span> <span class="o">=</span> <span class="n">Composition</span><span class="p">(</span><span class="n">element</span><span class="o">.</span><span class="n">symbol</span><span class="p">)</span>
        <span class="n">evolution</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="k">for</span> <span class="n">cc</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_critical_compositions</span><span class="p">(</span><span class="n">elcomp</span><span class="p">,</span> <span class="n">gccomp</span><span class="p">)[</span><span class="mi">1</span><span class="p">:]:</span>
            <span class="n">decomp_entries</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_decomposition</span><span class="p">(</span><span class="n">cc</span><span class="p">)</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span>
            <span class="n">decomp</span> <span class="o">=</span> <span class="p">[</span><span class="n">k</span><span class="o">.</span><span class="n">composition</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">decomp_entries</span><span class="p">]</span>
            <span class="n">rxn</span> <span class="o">=</span> <span class="n">Reaction</span><span class="p">([</span><span class="n">comp</span><span class="p">],</span> <span class="n">decomp</span> <span class="o">+</span> <span class="p">[</span><span class="n">elcomp</span><span class="p">])</span>
            <span class="n">rxn</span><span class="o">.</span><span class="n">normalize_to</span><span class="p">(</span><span class="n">comp</span><span class="p">)</span>
            <span class="n">c</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_composition_chempots</span><span class="p">(</span><span class="n">cc</span> <span class="o">+</span> <span class="n">elcomp</span> <span class="o">*</span> <span class="mf">1e-5</span><span class="p">)[</span><span class="n">element</span><span class="p">]</span>
            <span class="n">amt</span> <span class="o">=</span> <span class="o">-</span><span class="n">rxn</span><span class="o">.</span><span class="n">coeffs</span><span class="p">[</span><span class="n">rxn</span><span class="o">.</span><span class="n">all_comp</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">elcomp</span><span class="p">)]</span>
            <span class="n">evolution</span><span class="o">.</span><span class="n">append</span><span class="p">({</span><span class="s1">&#39;chempot&#39;</span><span class="p">:</span> <span class="n">c</span><span class="p">,</span>
                              <span class="s1">&#39;evolution&#39;</span><span class="p">:</span> <span class="n">amt</span><span class="p">,</span>
                              <span class="s1">&#39;element_reference&#39;</span><span class="p">:</span> <span class="n">elref</span><span class="p">,</span>
                              <span class="s1">&#39;reaction&#39;</span><span class="p">:</span> <span class="n">rxn</span><span class="p">,</span> <span class="s1">&#39;entries&#39;</span><span class="p">:</span> <span class="n">decomp_entries</span><span class="p">})</span>
        <span class="k">return</span> <span class="n">evolution</span></div>

<div class="viewcode-block" id="PhaseDiagram.get_chempot_range_map"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.PhaseDiagram.get_chempot_range_map">[docs]</a>    <span class="k">def</span> <span class="nf">get_chempot_range_map</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">elements</span><span class="p">,</span> <span class="n">referenced</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">joggle</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns a chemical potential range map for each stable entry.</span>

<span class="sd">        Args:</span>
<span class="sd">            elements: Sequence of elements to be considered as independent</span>
<span class="sd">                variables. E.g., if you want to show the stability ranges</span>
<span class="sd">                of all Li-Co-O phases wrt to uLi and uO, you will supply</span>
<span class="sd">                [Element(&quot;Li&quot;), Element(&quot;O&quot;)]</span>
<span class="sd">            referenced: If True, gives the results with a reference being the</span>
<span class="sd">                energy of the elemental phase. If False, gives absolute values.</span>
<span class="sd">            joggle (boolean): Whether to joggle the input to avoid precision</span>
<span class="sd">                errors.</span>

<span class="sd">        Returns:</span>
<span class="sd">            Returns a dict of the form {entry: [simplices]}. The list of</span>
<span class="sd">            simplices are the sides of the N-1 dim polytope bounding the</span>
<span class="sd">            allowable chemical potential range of each entry.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">all_chempots</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">pd</span> <span class="o">=</span> <span class="bp">self</span>
        <span class="n">facets</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">facets</span>
        <span class="k">for</span> <span class="n">facet</span> <span class="ow">in</span> <span class="n">facets</span><span class="p">:</span>
            <span class="n">chempots</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_facet_chempots</span><span class="p">(</span><span class="n">facet</span><span class="p">)</span>
            <span class="n">all_chempots</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="n">chempots</span><span class="p">[</span><span class="n">el</span><span class="p">]</span> <span class="k">for</span> <span class="n">el</span> <span class="ow">in</span> <span class="n">pd</span><span class="o">.</span><span class="n">elements</span><span class="p">])</span>
        <span class="n">inds</span> <span class="o">=</span> <span class="p">[</span><span class="n">pd</span><span class="o">.</span><span class="n">elements</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">el</span><span class="p">)</span> <span class="k">for</span> <span class="n">el</span> <span class="ow">in</span> <span class="n">elements</span><span class="p">]</span>
        <span class="n">el_energies</span> <span class="o">=</span> <span class="p">{</span><span class="n">el</span><span class="p">:</span> <span class="mf">0.0</span> <span class="k">for</span> <span class="n">el</span> <span class="ow">in</span> <span class="n">elements</span><span class="p">}</span>
        <span class="k">if</span> <span class="n">referenced</span><span class="p">:</span>
            <span class="n">el_energies</span> <span class="o">=</span> <span class="p">{</span><span class="n">el</span><span class="p">:</span> <span class="n">pd</span><span class="o">.</span><span class="n">el_refs</span><span class="p">[</span><span class="n">el</span><span class="p">]</span><span class="o">.</span><span class="n">energy_per_atom</span>
                           <span class="k">for</span> <span class="n">el</span> <span class="ow">in</span> <span class="n">elements</span><span class="p">}</span>
        <span class="n">chempot_ranges</span> <span class="o">=</span> <span class="n">collections</span><span class="o">.</span><span class="n">defaultdict</span><span class="p">(</span><span class="nb">list</span><span class="p">)</span>
        <span class="n">vertices</span> <span class="o">=</span> <span class="p">[</span><span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">elements</span><span class="p">)))]</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">all_chempots</span><span class="p">)</span> <span class="o">&gt;</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">elements</span><span class="p">):</span>
            <span class="n">vertices</span> <span class="o">=</span> <span class="n">get_facets</span><span class="p">(</span><span class="n">all_chempots</span><span class="p">,</span> <span class="n">joggle</span><span class="o">=</span><span class="n">joggle</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">ufacet</span> <span class="ow">in</span> <span class="n">vertices</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">combi</span> <span class="ow">in</span> <span class="n">itertools</span><span class="o">.</span><span class="n">combinations</span><span class="p">(</span><span class="n">ufacet</span><span class="p">,</span> <span class="mi">2</span><span class="p">):</span>
                <span class="n">data1</span> <span class="o">=</span> <span class="n">facets</span><span class="p">[</span><span class="n">combi</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span>
                <span class="n">data2</span> <span class="o">=</span> <span class="n">facets</span><span class="p">[</span><span class="n">combi</span><span class="p">[</span><span class="mi">1</span><span class="p">]]</span>
                <span class="n">common_ent_ind</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">data1</span><span class="p">)</span><span class="o">.</span><span class="n">intersection</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">data2</span><span class="p">))</span>
                <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">common_ent_ind</span><span class="p">)</span> <span class="o">==</span> <span class="nb">len</span><span class="p">(</span><span class="n">elements</span><span class="p">):</span>
                    <span class="n">common_entries</span> <span class="o">=</span> <span class="p">[</span><span class="n">pd</span><span class="o">.</span><span class="n">qhull_entries</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
                                      <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">common_ent_ind</span><span class="p">]</span>
                    <span class="n">data</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="n">all_chempots</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span>
                                      <span class="o">-</span> <span class="n">el_energies</span><span class="p">[</span><span class="n">pd</span><span class="o">.</span><span class="n">elements</span><span class="p">[</span><span class="n">j</span><span class="p">]]</span>
                                      <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="n">inds</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">combi</span><span class="p">])</span>
                    <span class="n">sim</span> <span class="o">=</span> <span class="n">Simplex</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
                    <span class="k">for</span> <span class="n">entry</span> <span class="ow">in</span> <span class="n">common_entries</span><span class="p">:</span>
                        <span class="n">chempot_ranges</span><span class="p">[</span><span class="n">entry</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">sim</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">chempot_ranges</span></div>

<div class="viewcode-block" id="PhaseDiagram.getmu_vertices_stability_phase"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.PhaseDiagram.getmu_vertices_stability_phase">[docs]</a>    <span class="k">def</span> <span class="nf">getmu_vertices_stability_phase</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">target_comp</span><span class="p">,</span> <span class="n">dep_elt</span><span class="p">,</span> <span class="n">tol_en</span><span class="o">=</span><span class="mf">1e-2</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        returns a set of chemical potentials corresponding to the vertices of</span>
<span class="sd">        the simplex in the chemical potential phase diagram.</span>
<span class="sd">        The simplex is built using all elements in the target_composition</span>
<span class="sd">        except dep_elt.</span>
<span class="sd">        The chemical potential of dep_elt is computed from the target</span>
<span class="sd">        composition energy.</span>
<span class="sd">        This method is useful to get the limiting conditions for</span>
<span class="sd">        defects computations for instance.</span>

<span class="sd">        Args:</span>
<span class="sd">            target_comp: A Composition object</span>
<span class="sd">            dep_elt: the element for which the chemical potential is computed</span>
<span class="sd">                from the energy of</span>
<span class="sd">            the stable phase at the target composition</span>
<span class="sd">            tol_en: a tolerance on the energy to set</span>

<span class="sd">        Returns:</span>
<span class="sd">             [{Element:mu}]: An array of conditions on simplex vertices for</span>
<span class="sd">             which each element has a chemical potential set to a given</span>
<span class="sd">             value. &quot;absolute&quot; values (i.e., not referenced to element energies)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">muref</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="bp">self</span><span class="o">.</span><span class="n">el_refs</span><span class="p">[</span><span class="n">e</span><span class="p">]</span><span class="o">.</span><span class="n">energy_per_atom</span>
                          <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">elements</span> <span class="k">if</span> <span class="n">e</span> <span class="o">!=</span> <span class="n">dep_elt</span><span class="p">])</span>
        <span class="n">chempot_ranges</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_chempot_range_map</span><span class="p">(</span>
            <span class="p">[</span><span class="n">e</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">elements</span> <span class="k">if</span> <span class="n">e</span> <span class="o">!=</span> <span class="n">dep_elt</span><span class="p">])</span>

        <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">elements</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">e</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">target_comp</span><span class="o">.</span><span class="n">elements</span><span class="p">:</span>
                <span class="n">target_comp</span> <span class="o">=</span> <span class="n">target_comp</span> <span class="o">+</span> <span class="n">Composition</span><span class="p">({</span><span class="n">e</span><span class="p">:</span> <span class="mf">0.0</span><span class="p">})</span>
        <span class="n">coeff</span> <span class="o">=</span> <span class="p">[</span><span class="o">-</span><span class="n">target_comp</span><span class="p">[</span><span class="n">e</span><span class="p">]</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">elements</span> <span class="k">if</span> <span class="n">e</span> <span class="o">!=</span> <span class="n">dep_elt</span><span class="p">]</span>
        <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">chempot_ranges</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">e</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">reduced_composition</span> <span class="o">==</span> \
                    <span class="n">target_comp</span><span class="o">.</span><span class="n">reduced_composition</span><span class="p">:</span>
                <span class="n">multiplicator</span> <span class="o">=</span> <span class="n">e</span><span class="o">.</span><span class="n">composition</span><span class="p">[</span><span class="n">dep_elt</span><span class="p">]</span> <span class="o">/</span> <span class="n">target_comp</span><span class="p">[</span><span class="n">dep_elt</span><span class="p">]</span>
                <span class="n">ef</span> <span class="o">=</span> <span class="n">e</span><span class="o">.</span><span class="n">energy</span> <span class="o">/</span> <span class="n">multiplicator</span>
                <span class="n">all_coords</span> <span class="o">=</span> <span class="p">[]</span>
                <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="n">chempot_ranges</span><span class="p">[</span><span class="n">e</span><span class="p">]:</span>
                    <span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">s</span><span class="o">.</span><span class="n">_coords</span><span class="p">:</span>
                        <span class="n">elts</span> <span class="o">=</span> <span class="p">[</span><span class="n">e</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">elements</span> <span class="k">if</span> <span class="n">e</span> <span class="o">!=</span> <span class="n">dep_elt</span><span class="p">]</span>
                        <span class="n">res</span> <span class="o">=</span> <span class="p">{}</span>
                        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">elts</span><span class="p">)):</span>
                            <span class="n">res</span><span class="p">[</span><span class="n">elts</span><span class="p">[</span><span class="n">i</span><span class="p">]]</span> <span class="o">=</span> <span class="n">v</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">+</span> <span class="n">muref</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
                        <span class="n">res</span><span class="p">[</span><span class="n">dep_elt</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">v</span> <span class="o">+</span> <span class="n">muref</span><span class="p">,</span> <span class="n">coeff</span><span class="p">)</span> <span class="o">+</span> <span class="n">ef</span><span class="p">)</span> <span class="o">/</span> <span class="n">target_comp</span><span class="p">[</span><span class="n">dep_elt</span><span class="p">]</span>
                        <span class="n">already_in</span> <span class="o">=</span> <span class="kc">False</span>
                        <span class="k">for</span> <span class="n">di</span> <span class="ow">in</span> <span class="n">all_coords</span><span class="p">:</span>
                            <span class="n">dict_equals</span> <span class="o">=</span> <span class="kc">True</span>
                            <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">di</span><span class="p">:</span>
                                <span class="k">if</span> <span class="nb">abs</span><span class="p">(</span><span class="n">di</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">-</span> <span class="n">res</span><span class="p">[</span><span class="n">k</span><span class="p">])</span> <span class="o">&gt;</span> <span class="n">tol_en</span><span class="p">:</span>
                                    <span class="n">dict_equals</span> <span class="o">=</span> <span class="kc">False</span>
                                    <span class="k">break</span>
                            <span class="k">if</span> <span class="n">dict_equals</span><span class="p">:</span>
                                <span class="n">already_in</span> <span class="o">=</span> <span class="kc">True</span>
                                <span class="k">break</span>
                        <span class="k">if</span> <span class="ow">not</span> <span class="n">already_in</span><span class="p">:</span>
                            <span class="n">all_coords</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">res</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">all_coords</span></div>

<div class="viewcode-block" id="PhaseDiagram.get_chempot_range_stability_phase"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.PhaseDiagram.get_chempot_range_stability_phase">[docs]</a>    <span class="k">def</span> <span class="nf">get_chempot_range_stability_phase</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">target_comp</span><span class="p">,</span> <span class="n">open_elt</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        returns a set of chemical potentials corresponding to the max and min</span>
<span class="sd">        chemical potential of the open element for a given composition. It is</span>
<span class="sd">        quite common to have for instance a ternary oxide (e.g., ABO3) for</span>
<span class="sd">        which you want to know what are the A and B chemical potential leading</span>
<span class="sd">        to the highest and lowest oxygen chemical potential (reducing and</span>
<span class="sd">        oxidizing conditions). This is useful for defect computations.</span>

<span class="sd">        Args:</span>
<span class="sd">            target_comp: A Composition object</span>
<span class="sd">            open_elt: Element that you want to constrain to be max or min</span>

<span class="sd">        Returns:</span>
<span class="sd">             {Element:(mu_min,mu_max)}: Chemical potentials are given in</span>
<span class="sd">             &quot;absolute&quot; values (i.e., not referenced to 0)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">muref</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="bp">self</span><span class="o">.</span><span class="n">el_refs</span><span class="p">[</span><span class="n">e</span><span class="p">]</span><span class="o">.</span><span class="n">energy_per_atom</span>
                          <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">elements</span> <span class="k">if</span> <span class="n">e</span> <span class="o">!=</span> <span class="n">open_elt</span><span class="p">])</span>
        <span class="n">chempot_ranges</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_chempot_range_map</span><span class="p">(</span>
            <span class="p">[</span><span class="n">e</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">elements</span> <span class="k">if</span> <span class="n">e</span> <span class="o">!=</span> <span class="n">open_elt</span><span class="p">])</span>
        <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">elements</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">e</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">target_comp</span><span class="o">.</span><span class="n">elements</span><span class="p">:</span>
                <span class="n">target_comp</span> <span class="o">=</span> <span class="n">target_comp</span> <span class="o">+</span> <span class="n">Composition</span><span class="p">({</span><span class="n">e</span><span class="p">:</span> <span class="mf">0.0</span><span class="p">})</span>
        <span class="n">coeff</span> <span class="o">=</span> <span class="p">[</span><span class="o">-</span><span class="n">target_comp</span><span class="p">[</span><span class="n">e</span><span class="p">]</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">elements</span> <span class="k">if</span> <span class="n">e</span> <span class="o">!=</span> <span class="n">open_elt</span><span class="p">]</span>
        <span class="n">max_open</span> <span class="o">=</span> <span class="o">-</span><span class="nb">float</span><span class="p">(</span><span class="s1">&#39;inf&#39;</span><span class="p">)</span>
        <span class="n">min_open</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="s1">&#39;inf&#39;</span><span class="p">)</span>
        <span class="n">max_mus</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="n">min_mus</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">chempot_ranges</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">e</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">reduced_composition</span> <span class="o">==</span> \
                    <span class="n">target_comp</span><span class="o">.</span><span class="n">reduced_composition</span><span class="p">:</span>
                <span class="n">multiplicator</span> <span class="o">=</span> <span class="n">e</span><span class="o">.</span><span class="n">composition</span><span class="p">[</span><span class="n">open_elt</span><span class="p">]</span> <span class="o">/</span> <span class="n">target_comp</span><span class="p">[</span><span class="n">open_elt</span><span class="p">]</span>
                <span class="n">ef</span> <span class="o">=</span> <span class="n">e</span><span class="o">.</span><span class="n">energy</span> <span class="o">/</span> <span class="n">multiplicator</span>
                <span class="n">all_coords</span> <span class="o">=</span> <span class="p">[]</span>
                <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="n">chempot_ranges</span><span class="p">[</span><span class="n">e</span><span class="p">]:</span>
                    <span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">s</span><span class="o">.</span><span class="n">_coords</span><span class="p">:</span>
                        <span class="n">all_coords</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">v</span><span class="p">)</span>
                        <span class="k">if</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">v</span> <span class="o">+</span> <span class="n">muref</span><span class="p">,</span> <span class="n">coeff</span><span class="p">)</span> <span class="o">+</span> <span class="n">ef</span><span class="p">)</span> <span class="o">/</span> <span class="n">target_comp</span><span class="p">[</span><span class="n">open_elt</span><span class="p">]</span> <span class="o">&gt;</span> <span class="n">max_open</span><span class="p">:</span>
                            <span class="n">max_open</span> <span class="o">=</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">v</span> <span class="o">+</span> <span class="n">muref</span><span class="p">,</span> <span class="n">coeff</span><span class="p">)</span> <span class="o">+</span> <span class="n">ef</span><span class="p">)</span> <span class="o">/</span> <span class="n">target_comp</span><span class="p">[</span><span class="n">open_elt</span><span class="p">]</span>
                            <span class="n">max_mus</span> <span class="o">=</span> <span class="n">v</span>
                        <span class="k">if</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">v</span> <span class="o">+</span> <span class="n">muref</span><span class="p">,</span> <span class="n">coeff</span><span class="p">)</span> <span class="o">+</span> <span class="n">ef</span><span class="p">)</span> <span class="o">/</span> <span class="n">target_comp</span><span class="p">[</span><span class="n">open_elt</span><span class="p">]</span> <span class="o">&lt;</span> <span class="n">min_open</span><span class="p">:</span>
                            <span class="n">min_open</span> <span class="o">=</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">v</span> <span class="o">+</span> <span class="n">muref</span><span class="p">,</span> <span class="n">coeff</span><span class="p">)</span> <span class="o">+</span> <span class="n">ef</span><span class="p">)</span> <span class="o">/</span> <span class="n">target_comp</span><span class="p">[</span><span class="n">open_elt</span><span class="p">]</span>
                            <span class="n">min_mus</span> <span class="o">=</span> <span class="n">v</span>
        <span class="n">elts</span> <span class="o">=</span> <span class="p">[</span><span class="n">e</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">elements</span> <span class="k">if</span> <span class="n">e</span> <span class="o">!=</span> <span class="n">open_elt</span><span class="p">]</span>
        <span class="n">res</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">elts</span><span class="p">)):</span>
            <span class="n">res</span><span class="p">[</span><span class="n">elts</span><span class="p">[</span><span class="n">i</span><span class="p">]]</span> <span class="o">=</span> <span class="p">(</span><span class="n">min_mus</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">+</span> <span class="n">muref</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">max_mus</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">+</span> <span class="n">muref</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
        <span class="n">res</span><span class="p">[</span><span class="n">open_elt</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">min_open</span><span class="p">,</span> <span class="n">max_open</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">res</span></div></div>


<div class="viewcode-block" id="GrandPotentialPhaseDiagram"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.GrandPotentialPhaseDiagram">[docs]</a><span class="k">class</span> <span class="nc">GrandPotentialPhaseDiagram</span><span class="p">(</span><span class="n">PhaseDiagram</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A class representing a Grand potential phase diagram. Grand potential phase</span>
<span class="sd">    diagrams are essentially phase diagrams that are open to one or more</span>
<span class="sd">    components. To construct such phase diagrams, the relevant free energy is</span>
<span class="sd">    the grand potential, which can be written as the Legendre transform of the</span>
<span class="sd">    Gibbs free energy as follows</span>

<span class="sd">    Grand potential = G - u_X N_X</span>

<span class="sd">    The algorithm is based on the work in the following papers:</span>

<span class="sd">    1. S. P. Ong, L. Wang, B. Kang, and G. Ceder, Li-Fe-P-O2 Phase Diagram from</span>
<span class="sd">       First Principles Calculations. Chem. Mater., 2008, 20(5), 1798-1807.</span>
<span class="sd">       doi:10.1021/cm702327g</span>

<span class="sd">    2. S. P. Ong, A. Jain, G. Hautier, B. Kang, G. Ceder, Thermal stabilities</span>
<span class="sd">       of delithiated olivine MPO4 (M=Fe, Mn) cathodes investigated using first</span>
<span class="sd">       principles calculations. Electrochem. Comm., 2010, 12(3), 427-430.</span>
<span class="sd">       doi:10.1016/j.elecom.2010.01.010</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">entries</span><span class="p">,</span> <span class="n">chempots</span><span class="p">,</span> <span class="n">elements</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Standard constructor for grand potential phase diagram.</span>

<span class="sd">        Args:</span>
<span class="sd">            entries ([PDEntry]): A list of PDEntry-like objects having an</span>
<span class="sd">                energy, energy_per_atom and composition.</span>
<span class="sd">            chempots {Element: float}: Specify the chemical potentials</span>
<span class="sd">                of the open elements.</span>
<span class="sd">            elements ([Element]): Optional list of elements in the phase</span>
<span class="sd">                diagram. If set to None, the elements are determined from</span>
<span class="sd">                the the entries themselves.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">elements</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">elements</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
            <span class="k">for</span> <span class="n">entry</span> <span class="ow">in</span> <span class="n">entries</span><span class="p">:</span>
                <span class="n">elements</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">entry</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">elements</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">chempots</span> <span class="o">=</span> <span class="p">{</span><span class="n">get_el_sp</span><span class="p">(</span><span class="n">el</span><span class="p">):</span> <span class="n">u</span> <span class="k">for</span> <span class="n">el</span><span class="p">,</span> <span class="n">u</span> <span class="ow">in</span> <span class="n">chempots</span><span class="o">.</span><span class="n">items</span><span class="p">()}</span>
        <span class="n">elements</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">elements</span><span class="p">)</span><span class="o">.</span><span class="n">difference</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">chempots</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span>
        <span class="n">all_entries</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">entries</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">elements</span><span class="p">)</span><span class="o">.</span><span class="n">intersection</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">elements</span><span class="p">)))</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">all_entries</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">GrandPotPDEntry</span><span class="p">(</span><span class="n">e</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">chempots</span><span class="p">))</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">all_entries</span><span class="p">,</span> <span class="n">elements</span><span class="p">)</span>

    <span class="k">def</span> <span class="fm">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">output</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">chemsys</span> <span class="o">=</span> <span class="s2">&quot;-&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="n">el</span><span class="o">.</span><span class="n">symbol</span> <span class="k">for</span> <span class="n">el</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">elements</span><span class="p">])</span>
        <span class="n">output</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">{}</span><span class="s2"> grand potential phase diagram with &quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">chemsys</span><span class="p">))</span>
        <span class="n">output</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">+=</span> <span class="s2">&quot;, &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="s2">&quot;u</span><span class="si">{}</span><span class="s2">=</span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">el</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span>
                                 <span class="k">for</span> <span class="n">el</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">chempots</span><span class="o">.</span><span class="n">items</span><span class="p">()])</span>
        <span class="n">output</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">{}</span><span class="s2"> stable phases: &quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">stable_entries</span><span class="p">)))</span>
        <span class="n">output</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;, &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="n">entry</span><span class="o">.</span><span class="n">name</span>
                                 <span class="k">for</span> <span class="n">entry</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">stable_entries</span><span class="p">]))</span>
        <span class="k">return</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">output</span><span class="p">)</span>

<div class="viewcode-block" id="GrandPotentialPhaseDiagram.as_dict"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.GrandPotentialPhaseDiagram.as_dict">[docs]</a>    <span class="k">def</span> <span class="nf">as_dict</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        :return: MSONable dict</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">{</span><span class="s2">&quot;@module&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__module__</span><span class="p">,</span>
                <span class="s2">&quot;@class&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
                <span class="s2">&quot;all_entries&quot;</span><span class="p">:</span> <span class="p">[</span><span class="n">e</span><span class="o">.</span><span class="n">as_dict</span><span class="p">()</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">all_entries</span><span class="p">],</span>
                <span class="s2">&quot;chempots&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">chempots</span><span class="p">,</span>
                <span class="s2">&quot;elements&quot;</span><span class="p">:</span> <span class="p">[</span><span class="n">e</span><span class="o">.</span><span class="n">as_dict</span><span class="p">()</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">elements</span><span class="p">]}</span></div>

<div class="viewcode-block" id="GrandPotentialPhaseDiagram.from_dict"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.GrandPotentialPhaseDiagram.from_dict">[docs]</a>    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">from_dict</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">d</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        :param d: Dict representation</span>
<span class="sd">        :return: GrandPotentialPhaseDiagram</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">entries</span> <span class="o">=</span> <span class="n">MontyDecoder</span><span class="p">()</span><span class="o">.</span><span class="n">process_decoded</span><span class="p">(</span><span class="n">d</span><span class="p">[</span><span class="s2">&quot;all_entries&quot;</span><span class="p">])</span>
        <span class="n">elements</span> <span class="o">=</span> <span class="n">MontyDecoder</span><span class="p">()</span><span class="o">.</span><span class="n">process_decoded</span><span class="p">(</span><span class="n">d</span><span class="p">[</span><span class="s2">&quot;elements&quot;</span><span class="p">])</span>
        <span class="k">return</span> <span class="bp">cls</span><span class="p">(</span><span class="n">entries</span><span class="p">,</span> <span class="n">d</span><span class="p">[</span><span class="s2">&quot;chempots&quot;</span><span class="p">],</span> <span class="n">elements</span><span class="p">)</span></div></div>


<div class="viewcode-block" id="CompoundPhaseDiagram"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.CompoundPhaseDiagram">[docs]</a><span class="k">class</span> <span class="nc">CompoundPhaseDiagram</span><span class="p">(</span><span class="n">PhaseDiagram</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Generates phase diagrams from compounds as terminations instead of</span>
<span class="sd">    elements.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="c1"># Tolerance for determining if amount of a composition is positive.</span>
    <span class="n">amount_tol</span> <span class="o">=</span> <span class="mf">1e-5</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">entries</span><span class="p">,</span> <span class="n">terminal_compositions</span><span class="p">,</span>
                 <span class="n">normalize_terminal_compositions</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Initializes a CompoundPhaseDiagram.</span>

<span class="sd">        Args:</span>
<span class="sd">            entries ([PDEntry]): Sequence of input entries. For example,</span>
<span class="sd">               if you want a Li2O-P2O5 phase diagram, you might have all</span>
<span class="sd">               Li-P-O entries as an input.</span>
<span class="sd">            terminal_compositions ([Composition]): Terminal compositions of</span>
<span class="sd">                phase space. In the Li2O-P2O5 example, these will be the</span>
<span class="sd">                Li2O and P2O5 compositions.</span>
<span class="sd">            normalize_terminal_compositions (bool): Whether to normalize the</span>
<span class="sd">                terminal compositions to a per atom basis. If normalized,</span>
<span class="sd">                the energy above hulls will be consistent</span>
<span class="sd">                for comparison across systems. Non-normalized terminals are</span>
<span class="sd">                more intuitive in terms of compositional breakdowns.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">original_entries</span> <span class="o">=</span> <span class="n">entries</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">terminal_compositions</span> <span class="o">=</span> <span class="n">terminal_compositions</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">normalize_terminals</span> <span class="o">=</span> <span class="n">normalize_terminal_compositions</span>
        <span class="p">(</span><span class="n">pentries</span><span class="p">,</span> <span class="n">species_mapping</span><span class="p">)</span> <span class="o">=</span> \
            <span class="bp">self</span><span class="o">.</span><span class="n">transform_entries</span><span class="p">(</span><span class="n">entries</span><span class="p">,</span> <span class="n">terminal_compositions</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">species_mapping</span> <span class="o">=</span> <span class="n">species_mapping</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span>
            <span class="n">pentries</span><span class="p">,</span> <span class="n">elements</span><span class="o">=</span><span class="n">species_mapping</span><span class="o">.</span><span class="n">values</span><span class="p">())</span>

<div class="viewcode-block" id="CompoundPhaseDiagram.transform_entries"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.CompoundPhaseDiagram.transform_entries">[docs]</a>    <span class="k">def</span> <span class="nf">transform_entries</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">entries</span><span class="p">,</span> <span class="n">terminal_compositions</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Method to transform all entries to the composition coordinate in the</span>
<span class="sd">        terminal compositions. If the entry does not fall within the space</span>
<span class="sd">        defined by the terminal compositions, they are excluded. For example,</span>
<span class="sd">        Li3PO4 is mapped into a Li2O:1.5, P2O5:0.5 composition. The terminal</span>
<span class="sd">        compositions are represented by DummySpecies.</span>

<span class="sd">        Args:</span>
<span class="sd">            entries: Sequence of all input entries</span>
<span class="sd">            terminal_compositions: Terminal compositions of phase space.</span>

<span class="sd">        Returns:</span>
<span class="sd">            Sequence of TransformedPDEntries falling within the phase space.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">new_entries</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">normalize_terminals</span><span class="p">:</span>
            <span class="n">fractional_comp</span> <span class="o">=</span> <span class="p">[</span><span class="n">c</span><span class="o">.</span><span class="n">fractional_composition</span>
                               <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">terminal_compositions</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">fractional_comp</span> <span class="o">=</span> <span class="n">terminal_compositions</span>

        <span class="c1"># Map terminal compositions to unique dummy species.</span>
        <span class="n">sp_mapping</span> <span class="o">=</span> <span class="n">collections</span><span class="o">.</span><span class="n">OrderedDict</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">comp</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">fractional_comp</span><span class="p">):</span>
            <span class="n">sp_mapping</span><span class="p">[</span><span class="n">comp</span><span class="p">]</span> <span class="o">=</span> <span class="n">DummySpecie</span><span class="p">(</span><span class="s2">&quot;X&quot;</span> <span class="o">+</span> <span class="nb">chr</span><span class="p">(</span><span class="mi">102</span> <span class="o">+</span> <span class="n">i</span><span class="p">))</span>

        <span class="k">for</span> <span class="n">entry</span> <span class="ow">in</span> <span class="n">entries</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">rxn</span> <span class="o">=</span> <span class="n">Reaction</span><span class="p">(</span><span class="n">fractional_comp</span><span class="p">,</span> <span class="p">[</span><span class="n">entry</span><span class="o">.</span><span class="n">composition</span><span class="p">])</span>
                <span class="n">rxn</span><span class="o">.</span><span class="n">normalize_to</span><span class="p">(</span><span class="n">entry</span><span class="o">.</span><span class="n">composition</span><span class="p">)</span>
                <span class="c1"># We only allow reactions that have positive amounts of</span>
                <span class="c1"># reactants.</span>
                <span class="k">if</span> <span class="nb">all</span><span class="p">([</span><span class="n">rxn</span><span class="o">.</span><span class="n">get_coeff</span><span class="p">(</span><span class="n">comp</span><span class="p">)</span> <span class="o">&lt;=</span> <span class="n">CompoundPhaseDiagram</span><span class="o">.</span><span class="n">amount_tol</span>
                        <span class="k">for</span> <span class="n">comp</span> <span class="ow">in</span> <span class="n">fractional_comp</span><span class="p">]):</span>
                    <span class="n">newcomp</span> <span class="o">=</span> <span class="p">{</span><span class="n">sp_mapping</span><span class="p">[</span><span class="n">comp</span><span class="p">]:</span> <span class="o">-</span><span class="n">rxn</span><span class="o">.</span><span class="n">get_coeff</span><span class="p">(</span><span class="n">comp</span><span class="p">)</span>
                               <span class="k">for</span> <span class="n">comp</span> <span class="ow">in</span> <span class="n">fractional_comp</span><span class="p">}</span>
                    <span class="n">newcomp</span> <span class="o">=</span> <span class="p">{</span><span class="n">k</span><span class="p">:</span> <span class="n">v</span> <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">newcomp</span><span class="o">.</span><span class="n">items</span><span class="p">()</span>
                               <span class="k">if</span> <span class="n">v</span> <span class="o">&gt;</span> <span class="n">CompoundPhaseDiagram</span><span class="o">.</span><span class="n">amount_tol</span><span class="p">}</span>
                    <span class="n">transformed_entry</span> <span class="o">=</span> \
                        <span class="n">TransformedPDEntry</span><span class="p">(</span><span class="n">Composition</span><span class="p">(</span><span class="n">newcomp</span><span class="p">),</span> <span class="n">entry</span><span class="p">)</span>
                    <span class="n">new_entries</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">transformed_entry</span><span class="p">)</span>
            <span class="k">except</span> <span class="n">ReactionError</span><span class="p">:</span>
                <span class="c1"># If the reaction can&#39;t be balanced, the entry does not fall</span>
                <span class="c1"># into the phase space. We ignore them.</span>
                <span class="k">pass</span>
        <span class="k">return</span> <span class="n">new_entries</span><span class="p">,</span> <span class="n">sp_mapping</span></div>

<div class="viewcode-block" id="CompoundPhaseDiagram.as_dict"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.CompoundPhaseDiagram.as_dict">[docs]</a>    <span class="k">def</span> <span class="nf">as_dict</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        :return: MSONable dict</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">{</span>
            <span class="s2">&quot;@module&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__module__</span><span class="p">,</span>
            <span class="s2">&quot;@class&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
            <span class="s2">&quot;original_entries&quot;</span><span class="p">:</span> <span class="p">[</span><span class="n">e</span><span class="o">.</span><span class="n">as_dict</span><span class="p">()</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">original_entries</span><span class="p">],</span>
            <span class="s2">&quot;terminal_compositions&quot;</span><span class="p">:</span> <span class="p">[</span><span class="n">c</span><span class="o">.</span><span class="n">as_dict</span><span class="p">()</span>
                                      <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">terminal_compositions</span><span class="p">],</span>
            <span class="s2">&quot;normalize_terminal_compositions&quot;</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">normalize_terminals</span><span class="p">}</span></div>

<div class="viewcode-block" id="CompoundPhaseDiagram.from_dict"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.CompoundPhaseDiagram.from_dict">[docs]</a>    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">from_dict</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">d</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        :param d: Dict Representation</span>
<span class="sd">        :return: CompoundPhaseDiagram</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">dec</span> <span class="o">=</span> <span class="n">MontyDecoder</span><span class="p">()</span>
        <span class="n">entries</span> <span class="o">=</span> <span class="n">dec</span><span class="o">.</span><span class="n">process_decoded</span><span class="p">(</span><span class="n">d</span><span class="p">[</span><span class="s2">&quot;original_entries&quot;</span><span class="p">])</span>
        <span class="n">terminal_compositions</span> <span class="o">=</span> <span class="n">dec</span><span class="o">.</span><span class="n">process_decoded</span><span class="p">(</span><span class="n">d</span><span class="p">[</span><span class="s2">&quot;terminal_compositions&quot;</span><span class="p">])</span>
        <span class="k">return</span> <span class="bp">cls</span><span class="p">(</span><span class="n">entries</span><span class="p">,</span> <span class="n">terminal_compositions</span><span class="p">,</span>
                   <span class="n">d</span><span class="p">[</span><span class="s2">&quot;normalize_terminal_compositions&quot;</span><span class="p">])</span></div></div>


<div class="viewcode-block" id="ReactionDiagram"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.ReactionDiagram">[docs]</a><span class="k">class</span> <span class="nc">ReactionDiagram</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Analyzes the possible reactions between a pair of compounds, e.g.,</span>
<span class="sd">    an electrolyte and an electrode.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">entry1</span><span class="p">,</span> <span class="n">entry2</span><span class="p">,</span> <span class="n">all_entries</span><span class="p">,</span> <span class="n">tol</span><span class="o">=</span><span class="mf">1e-4</span><span class="p">,</span>
                 <span class="n">float_fmt</span><span class="o">=</span><span class="s2">&quot;</span><span class="si">%.4f</span><span class="s2">&quot;</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Args:</span>
<span class="sd">            entry1 (ComputedEntry): Entry for 1st component. Note that</span>
<span class="sd">                corrections, if any, must already be pre-applied. This is to</span>
<span class="sd">                give flexibility for different kinds of corrections, e.g.,</span>
<span class="sd">                if a particular entry is fitted to an experimental data (such</span>
<span class="sd">                as EC molecule).</span>
<span class="sd">            entry2 (ComputedEntry): Entry for 2nd component. Note that</span>
<span class="sd">                corrections must already be pre-applied. This is to</span>
<span class="sd">                give flexibility for different kinds of corrections, e.g.,</span>
<span class="sd">                if a particular entry is fitted to an experimental data (such</span>
<span class="sd">                as EC molecule).</span>
<span class="sd">            all_entries ([ComputedEntry]): All other entries to be</span>
<span class="sd">                considered in the analysis. Note that corrections, if any,</span>
<span class="sd">                must already be pre-applied.</span>
<span class="sd">            tol (float): Tolerance to be used to determine validity of reaction.</span>
<span class="sd">            float_fmt (str): Formatting string to be applied to all floats.</span>
<span class="sd">                Determines number of decimal places in reaction string.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">elements</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="p">[</span><span class="n">entry1</span><span class="p">,</span> <span class="n">entry2</span><span class="p">]:</span>
            <span class="n">elements</span><span class="o">.</span><span class="n">update</span><span class="p">([</span><span class="n">el</span><span class="o">.</span><span class="n">symbol</span> <span class="k">for</span> <span class="n">el</span> <span class="ow">in</span> <span class="n">e</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">elements</span><span class="p">])</span>

        <span class="n">elements</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">elements</span><span class="p">)</span>  <span class="c1"># Fix elements to ensure order.</span>

        <span class="n">comp_vec1</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">entry1</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">get_atomic_fraction</span><span class="p">(</span><span class="n">el</span><span class="p">)</span>
                              <span class="k">for</span> <span class="n">el</span> <span class="ow">in</span> <span class="n">elements</span><span class="p">])</span>
        <span class="n">comp_vec2</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">entry2</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">get_atomic_fraction</span><span class="p">(</span><span class="n">el</span><span class="p">)</span>
                              <span class="k">for</span> <span class="n">el</span> <span class="ow">in</span> <span class="n">elements</span><span class="p">])</span>
        <span class="n">r1</span> <span class="o">=</span> <span class="n">entry1</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">reduced_composition</span>
        <span class="n">r2</span> <span class="o">=</span> <span class="n">entry2</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">reduced_composition</span>

        <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%d</span><span class="s2"> total entries.&quot;</span> <span class="o">%</span> <span class="nb">len</span><span class="p">(</span><span class="n">all_entries</span><span class="p">))</span>

        <span class="n">pd</span> <span class="o">=</span> <span class="n">PhaseDiagram</span><span class="p">(</span><span class="n">all_entries</span> <span class="o">+</span> <span class="p">[</span><span class="n">entry1</span><span class="p">,</span> <span class="n">entry2</span><span class="p">])</span>
        <span class="n">terminal_formulas</span> <span class="o">=</span> <span class="p">[</span><span class="n">entry1</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">reduced_formula</span><span class="p">,</span>
                             <span class="n">entry2</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">reduced_formula</span><span class="p">]</span>

        <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%d</span><span class="s2"> stable entries&quot;</span> <span class="o">%</span> <span class="nb">len</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">stable_entries</span><span class="p">))</span>
        <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%d</span><span class="s2"> facets&quot;</span> <span class="o">%</span> <span class="nb">len</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">facets</span><span class="p">))</span>
        <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%d</span><span class="s2"> qhull_entries&quot;</span> <span class="o">%</span> <span class="nb">len</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">qhull_entries</span><span class="p">))</span>

        <span class="n">rxn_entries</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">done</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="k">def</span> <span class="nf">fmt</span><span class="p">(</span><span class="n">fl</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">float_fmt</span> <span class="o">%</span> <span class="n">fl</span>

        <span class="k">for</span> <span class="n">facet</span> <span class="ow">in</span> <span class="n">pd</span><span class="o">.</span><span class="n">facets</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">face</span> <span class="ow">in</span> <span class="n">itertools</span><span class="o">.</span><span class="n">combinations</span><span class="p">(</span><span class="n">facet</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">facet</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">):</span>
                <span class="n">face_entries</span> <span class="o">=</span> <span class="p">[</span><span class="n">pd</span><span class="o">.</span><span class="n">qhull_entries</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">face</span><span class="p">]</span>

                <span class="k">if</span> <span class="nb">any</span><span class="p">([</span><span class="n">e</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">reduced_formula</span> <span class="ow">in</span> <span class="n">terminal_formulas</span>
                        <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">face_entries</span><span class="p">]):</span>
                    <span class="k">continue</span>

                <span class="k">try</span><span class="p">:</span>

                    <span class="n">m</span> <span class="o">=</span> <span class="p">[]</span>
                    <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">face_entries</span><span class="p">:</span>
                        <span class="n">m</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="n">e</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">get_atomic_fraction</span><span class="p">(</span><span class="n">el</span><span class="p">)</span>
                                  <span class="k">for</span> <span class="n">el</span> <span class="ow">in</span> <span class="n">elements</span><span class="p">])</span>
                    <span class="n">m</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">comp_vec2</span> <span class="o">-</span> <span class="n">comp_vec1</span><span class="p">)</span>
                    <span class="n">m</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">m</span><span class="p">)</span><span class="o">.</span><span class="n">T</span>
                    <span class="n">coeffs</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">solve</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">comp_vec2</span><span class="p">)</span>

                    <span class="n">x</span> <span class="o">=</span> <span class="n">coeffs</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>

                    <span class="k">if</span> <span class="nb">all</span><span class="p">([</span><span class="n">c</span> <span class="o">&gt;=</span> <span class="o">-</span><span class="n">tol</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">coeffs</span><span class="p">])</span> <span class="ow">and</span> \
                            <span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="nb">sum</span><span class="p">(</span><span class="n">coeffs</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">&lt;</span> <span class="n">tol</span><span class="p">)</span> <span class="ow">and</span> \
                            <span class="p">(</span><span class="n">tol</span> <span class="o">&lt;</span> <span class="n">x</span> <span class="o">&lt;</span> <span class="mi">1</span> <span class="o">-</span> <span class="n">tol</span><span class="p">):</span>

                        <span class="n">c1</span> <span class="o">=</span> <span class="n">x</span> <span class="o">/</span> <span class="n">r1</span><span class="o">.</span><span class="n">num_atoms</span>
                        <span class="n">c2</span> <span class="o">=</span> <span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="n">x</span><span class="p">)</span> <span class="o">/</span> <span class="n">r2</span><span class="o">.</span><span class="n">num_atoms</span>
                        <span class="n">factor</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">/</span> <span class="p">(</span><span class="n">c1</span> <span class="o">+</span> <span class="n">c2</span><span class="p">)</span>

                        <span class="n">c1</span> <span class="o">*=</span> <span class="n">factor</span>
                        <span class="n">c2</span> <span class="o">*=</span> <span class="n">factor</span>

                        <span class="c1"># Avoid duplicate reactions.</span>
                        <span class="k">if</span> <span class="nb">any</span><span class="p">([</span><span class="n">np</span><span class="o">.</span><span class="n">allclose</span><span class="p">([</span><span class="n">c1</span><span class="p">,</span> <span class="n">c2</span><span class="p">],</span> <span class="n">cc</span><span class="p">)</span> <span class="k">for</span> <span class="n">cc</span> <span class="ow">in</span> <span class="n">done</span><span class="p">]):</span>
                            <span class="k">continue</span>

                        <span class="n">done</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">c1</span><span class="p">,</span> <span class="n">c2</span><span class="p">))</span>

                        <span class="n">rxn_str</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="si">%s</span><span class="s2"> </span><span class="si">%s</span><span class="s2"> + </span><span class="si">%s</span><span class="s2"> </span><span class="si">%s</span><span class="s2"> -&gt; &quot;</span> <span class="o">%</span> <span class="p">(</span>
                            <span class="n">fmt</span><span class="p">(</span><span class="n">c1</span><span class="p">),</span> <span class="n">r1</span><span class="o">.</span><span class="n">reduced_formula</span><span class="p">,</span>
                            <span class="n">fmt</span><span class="p">(</span><span class="n">c2</span><span class="p">),</span> <span class="n">r2</span><span class="o">.</span><span class="n">reduced_formula</span><span class="p">)</span>
                        <span class="n">products</span> <span class="o">=</span> <span class="p">[]</span>
                        <span class="n">product_entries</span> <span class="o">=</span> <span class="p">[]</span>

                        <span class="n">energy</span> <span class="o">=</span> <span class="o">-</span> <span class="p">(</span><span class="n">x</span> <span class="o">*</span> <span class="n">entry1</span><span class="o">.</span><span class="n">energy_per_atom</span> <span class="o">+</span>
                                    <span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="n">x</span><span class="p">)</span> <span class="o">*</span> <span class="n">entry2</span><span class="o">.</span><span class="n">energy_per_atom</span><span class="p">)</span>

                        <span class="k">for</span> <span class="n">c</span><span class="p">,</span> <span class="n">e</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">coeffs</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">],</span> <span class="n">face_entries</span><span class="p">):</span>
                            <span class="k">if</span> <span class="n">c</span> <span class="o">&gt;</span> <span class="n">tol</span><span class="p">:</span>
                                <span class="n">r</span> <span class="o">=</span> <span class="n">e</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">reduced_composition</span>
                                <span class="n">products</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%s</span><span class="s2"> </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span>
                                    <span class="n">fmt</span><span class="p">(</span><span class="n">c</span> <span class="o">/</span> <span class="n">r</span><span class="o">.</span><span class="n">num_atoms</span> <span class="o">*</span> <span class="n">factor</span><span class="p">),</span>
                                    <span class="n">r</span><span class="o">.</span><span class="n">reduced_formula</span><span class="p">))</span>
                                <span class="n">product_entries</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">c</span><span class="p">,</span> <span class="n">e</span><span class="p">))</span>
                                <span class="n">energy</span> <span class="o">+=</span> <span class="n">c</span> <span class="o">*</span> <span class="n">e</span><span class="o">.</span><span class="n">energy_per_atom</span>

                        <span class="n">rxn_str</span> <span class="o">+=</span> <span class="s2">&quot; + &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">products</span><span class="p">)</span>
                        <span class="n">comp</span> <span class="o">=</span> <span class="n">x</span> <span class="o">*</span> <span class="n">comp_vec1</span> <span class="o">+</span> <span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="n">x</span><span class="p">)</span> <span class="o">*</span> <span class="n">comp_vec2</span>
                        <span class="n">entry</span> <span class="o">=</span> <span class="n">PDEntry</span><span class="p">(</span>
                            <span class="n">Composition</span><span class="p">(</span><span class="nb">dict</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="n">elements</span><span class="p">,</span> <span class="n">comp</span><span class="p">))),</span>
                            <span class="n">energy</span><span class="o">=</span><span class="n">energy</span><span class="p">,</span> <span class="n">attribute</span><span class="o">=</span><span class="n">rxn_str</span><span class="p">)</span>
                        <span class="n">entry</span><span class="o">.</span><span class="n">decomposition</span> <span class="o">=</span> <span class="n">product_entries</span>
                        <span class="n">rxn_entries</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">entry</span><span class="p">)</span>
                <span class="k">except</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">LinAlgError</span><span class="p">:</span>
                    <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;Reactants = </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="s2">&quot;, &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span>
                        <span class="n">entry1</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">reduced_formula</span><span class="p">,</span>
                        <span class="n">entry2</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">reduced_formula</span><span class="p">])))</span>
                    <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;Products = </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span>
                        <span class="s2">&quot;, &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="n">e</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">reduced_formula</span>
                                   <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">face_entries</span><span class="p">])))</span>

        <span class="n">rxn_entries</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">rxn_entries</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">e</span><span class="p">:</span> <span class="n">e</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">reverse</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">entry1</span> <span class="o">=</span> <span class="n">entry1</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">entry2</span> <span class="o">=</span> <span class="n">entry2</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">rxn_entries</span> <span class="o">=</span> <span class="n">rxn_entries</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">labels</span> <span class="o">=</span> <span class="n">collections</span><span class="o">.</span><span class="n">OrderedDict</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">e</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">rxn_entries</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">labels</span><span class="p">[</span><span class="nb">str</span><span class="p">(</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)]</span> <span class="o">=</span> <span class="n">e</span><span class="o">.</span><span class="n">attribute</span>
            <span class="n">e</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">all_entries</span> <span class="o">=</span> <span class="n">all_entries</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pd</span> <span class="o">=</span> <span class="n">pd</span>

<div class="viewcode-block" id="ReactionDiagram.get_compound_pd"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.ReactionDiagram.get_compound_pd">[docs]</a>    <span class="k">def</span> <span class="nf">get_compound_pd</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get the CompoundPhaseDiagram object, which can then be used for</span>
<span class="sd">        plotting.</span>

<span class="sd">        Returns:</span>
<span class="sd">            (CompoundPhaseDiagram)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># For this plot, since the reactions are reported in formation</span>
        <span class="c1"># energies, we need to set the energies of the terminal compositions</span>
        <span class="c1"># to 0. So we make create copies with 0 energy.</span>
        <span class="n">entry1</span> <span class="o">=</span> <span class="n">PDEntry</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">entry1</span><span class="o">.</span><span class="n">composition</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
        <span class="n">entry2</span> <span class="o">=</span> <span class="n">PDEntry</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">entry2</span><span class="o">.</span><span class="n">composition</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>

        <span class="n">cpd</span> <span class="o">=</span> <span class="n">CompoundPhaseDiagram</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">rxn_entries</span> <span class="o">+</span> <span class="p">[</span><span class="n">entry1</span><span class="p">,</span> <span class="n">entry2</span><span class="p">],</span>
            <span class="p">[</span><span class="n">Composition</span><span class="p">(</span><span class="n">entry1</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">reduced_formula</span><span class="p">),</span>
             <span class="n">Composition</span><span class="p">(</span><span class="n">entry2</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">reduced_formula</span><span class="p">)],</span>
            <span class="n">normalize_terminal_compositions</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">cpd</span></div></div>


<div class="viewcode-block" id="PhaseDiagramError"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.PhaseDiagramError">[docs]</a><span class="k">class</span> <span class="nc">PhaseDiagramError</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    An exception class for Phase Diagram generation.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">pass</span></div>


<div class="viewcode-block" id="get_facets"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.get_facets">[docs]</a><span class="k">def</span> <span class="nf">get_facets</span><span class="p">(</span><span class="n">qhull_data</span><span class="p">,</span> <span class="n">joggle</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Get the simplex facets for the Convex hull.</span>

<span class="sd">    Args:</span>
<span class="sd">        qhull_data (np.ndarray): The data from which to construct the convex</span>
<span class="sd">            hull as a Nxd array (N being number of data points and d being the</span>
<span class="sd">            dimension)</span>
<span class="sd">        joggle (boolean): Whether to joggle the input to avoid precision</span>
<span class="sd">            errors.</span>

<span class="sd">    Returns:</span>
<span class="sd">        List of simplices of the Convex Hull.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">joggle</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">ConvexHull</span><span class="p">(</span><span class="n">qhull_data</span><span class="p">,</span> <span class="n">qhull_options</span><span class="o">=</span><span class="s2">&quot;QJ i&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">simplices</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">ConvexHull</span><span class="p">(</span><span class="n">qhull_data</span><span class="p">,</span> <span class="n">qhull_options</span><span class="o">=</span><span class="s2">&quot;Qt i&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">simplices</span></div>


<div class="viewcode-block" id="PDPlotter"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.PDPlotter">[docs]</a><span class="k">class</span> <span class="nc">PDPlotter</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A plotter class for phase diagrams.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">phasediagram</span><span class="p">,</span> <span class="n">show_unstable</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="o">**</span><span class="n">plotkwargs</span><span class="p">):</span>
        <span class="sa">r</span><span class="sd">&quot;&quot;&quot;</span>

<span class="sd">        Args:</span>
<span class="sd">            phasediagram: PhaseDiagram object.</span>
<span class="sd">            show_unstable (float): Whether unstable phases will be plotted as</span>
<span class="sd">                well as red crosses. If a number &gt; 0 is entered, all phases with</span>
<span class="sd">                ehull &lt; show_unstable will be shown.</span>
<span class="sd">            **plotkwargs: Keyword args passed to matplotlib.pyplot.plot. Can</span>
<span class="sd">                be used to customize markers etc. If not set, the default is</span>
<span class="sd">                {</span>
<span class="sd">                    &quot;markerfacecolor&quot;: (0.2157, 0.4941, 0.7216),</span>
<span class="sd">                    &quot;markersize&quot;: 10,</span>
<span class="sd">                    &quot;linewidth&quot;: 3</span>
<span class="sd">                }</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># note: palettable imports matplotlib</span>
        <span class="kn">from</span> <span class="nn">palettable.colorbrewer.qualitative</span> <span class="kn">import</span> <span class="n">Set1_3</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_pd</span> <span class="o">=</span> <span class="n">phasediagram</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_dim</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_pd</span><span class="o">.</span><span class="n">elements</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_dim</span> <span class="o">&gt;</span> <span class="mi">4</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Only 1-4 components supported!&quot;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">lines</span> <span class="o">=</span> <span class="n">uniquelines</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_pd</span><span class="o">.</span><span class="n">facets</span><span class="p">)</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_dim</span> <span class="o">&gt;</span> <span class="mi">1</span> <span class="k">else</span> \
            <span class="p">[[</span><span class="bp">self</span><span class="o">.</span><span class="n">_pd</span><span class="o">.</span><span class="n">facets</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pd</span><span class="o">.</span><span class="n">facets</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">]]]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">show_unstable</span> <span class="o">=</span> <span class="n">show_unstable</span>
        <span class="n">colors</span> <span class="o">=</span> <span class="n">Set1_3</span><span class="o">.</span><span class="n">mpl_colors</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">plotkwargs</span> <span class="o">=</span> <span class="n">plotkwargs</span> <span class="ow">or</span> <span class="p">{</span>
            <span class="s2">&quot;markerfacecolor&quot;</span><span class="p">:</span> <span class="n">colors</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span>
            <span class="s2">&quot;markersize&quot;</span><span class="p">:</span> <span class="mi">10</span><span class="p">,</span>
            <span class="s2">&quot;linewidth&quot;</span><span class="p">:</span> <span class="mi">3</span>
        <span class="p">}</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">pd_plot_data</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Plot data for phase diagram.</span>
<span class="sd">        2-comp - Full hull with energies</span>
<span class="sd">        3/4-comp - Projection into 2D or 3D Gibbs triangle.</span>

<span class="sd">        Returns:</span>
<span class="sd">            (lines, stable_entries, unstable_entries):</span>
<span class="sd">            - lines is a list of list of coordinates for lines in the PD.</span>
<span class="sd">            - stable_entries is a {coordinate : entry} for each stable node</span>
<span class="sd">            in the phase diagram. (Each coordinate can only have one</span>
<span class="sd">            stable phase)</span>
<span class="sd">            - unstable_entries is a {entry: coordinates} for all unstable</span>
<span class="sd">            nodes in the phase diagram.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">pd</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pd</span>
        <span class="n">entries</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">qhull_entries</span>
        <span class="n">data</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">qhull_data</span><span class="p">)</span>
        <span class="n">lines</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">stable_entries</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">lines</span><span class="p">:</span>
            <span class="n">entry1</span> <span class="o">=</span> <span class="n">entries</span><span class="p">[</span><span class="n">line</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span>
            <span class="n">entry2</span> <span class="o">=</span> <span class="n">entries</span><span class="p">[</span><span class="n">line</span><span class="p">[</span><span class="mi">1</span><span class="p">]]</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_dim</span> <span class="o">&lt;</span> <span class="mi">3</span><span class="p">:</span>
                <span class="n">x</span> <span class="o">=</span> <span class="p">[</span><span class="n">data</span><span class="p">[</span><span class="n">line</span><span class="p">[</span><span class="mi">0</span><span class="p">]][</span><span class="mi">0</span><span class="p">],</span> <span class="n">data</span><span class="p">[</span><span class="n">line</span><span class="p">[</span><span class="mi">1</span><span class="p">]][</span><span class="mi">0</span><span class="p">]]</span>
                <span class="n">y</span> <span class="o">=</span> <span class="p">[</span><span class="n">pd</span><span class="o">.</span><span class="n">get_form_energy_per_atom</span><span class="p">(</span><span class="n">entry1</span><span class="p">),</span>
                     <span class="n">pd</span><span class="o">.</span><span class="n">get_form_energy_per_atom</span><span class="p">(</span><span class="n">entry2</span><span class="p">)]</span>
                <span class="n">coord</span> <span class="o">=</span> <span class="p">[</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">]</span>
            <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">_dim</span> <span class="o">==</span> <span class="mi">3</span><span class="p">:</span>
                <span class="n">coord</span> <span class="o">=</span> <span class="n">triangular_coord</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="n">line</span><span class="p">,</span> <span class="mi">0</span><span class="p">:</span><span class="mi">2</span><span class="p">])</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">coord</span> <span class="o">=</span> <span class="n">tet_coord</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="n">line</span><span class="p">,</span> <span class="mi">0</span><span class="p">:</span><span class="mi">3</span><span class="p">])</span>
            <span class="n">lines</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">coord</span><span class="p">)</span>
            <span class="n">labelcoord</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="o">*</span><span class="n">coord</span><span class="p">))</span>
            <span class="n">stable_entries</span><span class="p">[</span><span class="n">labelcoord</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span> <span class="o">=</span> <span class="n">entry1</span>
            <span class="n">stable_entries</span><span class="p">[</span><span class="n">labelcoord</span><span class="p">[</span><span class="mi">1</span><span class="p">]]</span> <span class="o">=</span> <span class="n">entry2</span>

        <span class="n">all_entries</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">all_entries</span>
        <span class="n">all_data</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">all_entries_hulldata</span><span class="p">)</span>
        <span class="n">unstable_entries</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">()</span>
        <span class="n">stable</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">stable_entries</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">all_entries</span><span class="p">)):</span>
            <span class="n">entry</span> <span class="o">=</span> <span class="n">all_entries</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">entry</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">stable</span><span class="p">:</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_dim</span> <span class="o">&lt;</span> <span class="mi">3</span><span class="p">:</span>
                    <span class="n">x</span> <span class="o">=</span> <span class="p">[</span><span class="n">all_data</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">0</span><span class="p">],</span> <span class="n">all_data</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">0</span><span class="p">]]</span>
                    <span class="n">y</span> <span class="o">=</span> <span class="p">[</span><span class="n">pd</span><span class="o">.</span><span class="n">get_form_energy_per_atom</span><span class="p">(</span><span class="n">entry</span><span class="p">),</span>
                         <span class="n">pd</span><span class="o">.</span><span class="n">get_form_energy_per_atom</span><span class="p">(</span><span class="n">entry</span><span class="p">)]</span>
                    <span class="n">coord</span> <span class="o">=</span> <span class="p">[</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">]</span>
                <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">_dim</span> <span class="o">==</span> <span class="mi">3</span><span class="p">:</span>
                    <span class="n">coord</span> <span class="o">=</span> <span class="n">triangular_coord</span><span class="p">([</span><span class="n">all_data</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="mi">0</span><span class="p">:</span><span class="mi">2</span><span class="p">],</span>
                                              <span class="n">all_data</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="mi">0</span><span class="p">:</span><span class="mi">2</span><span class="p">]])</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">coord</span> <span class="o">=</span> <span class="n">tet_coord</span><span class="p">([</span><span class="n">all_data</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="mi">0</span><span class="p">:</span><span class="mi">3</span><span class="p">],</span> <span class="n">all_data</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="mi">0</span><span class="p">:</span><span class="mi">3</span><span class="p">],</span>
                                       <span class="n">all_data</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="mi">0</span><span class="p">:</span><span class="mi">3</span><span class="p">]])</span>
                <span class="n">labelcoord</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="o">*</span><span class="n">coord</span><span class="p">))</span>
                <span class="n">unstable_entries</span><span class="p">[</span><span class="n">entry</span><span class="p">]</span> <span class="o">=</span> <span class="n">labelcoord</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>

        <span class="k">return</span> <span class="n">lines</span><span class="p">,</span> <span class="n">stable_entries</span><span class="p">,</span> <span class="n">unstable_entries</span>

<div class="viewcode-block" id="PDPlotter.get_plot"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.PDPlotter.get_plot">[docs]</a>    <span class="k">def</span> <span class="nf">get_plot</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">label_stable</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">label_unstable</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">ordering</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">energy_colormap</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">process_attributes</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">plt</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        :param label_stable: Whether to label stable compounds.</span>
<span class="sd">        :param label_unstable: Whether to label unstable compounds.</span>
<span class="sd">        :param ordering: Ordering of vertices.</span>
<span class="sd">        :param energy_colormap: Colormap for coloring energy.</span>
<span class="sd">        :param process_attributes: Whether to process the attributes.</span>
<span class="sd">        :param plt: Existing plt object if plotting multiple phase diagrams.</span>
<span class="sd">        :return: matplotlib.pyplot.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_dim</span> <span class="o">&lt;</span> <span class="mi">4</span><span class="p">:</span>
            <span class="n">plt</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_2d_plot</span><span class="p">(</span><span class="n">label_stable</span><span class="p">,</span> <span class="n">label_unstable</span><span class="p">,</span> <span class="n">ordering</span><span class="p">,</span>
                                    <span class="n">energy_colormap</span><span class="p">,</span> <span class="n">plt</span><span class="o">=</span><span class="n">plt</span><span class="p">,</span>
                                    <span class="n">process_attributes</span><span class="o">=</span><span class="n">process_attributes</span><span class="p">)</span>
        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">_dim</span> <span class="o">==</span> <span class="mi">4</span><span class="p">:</span>
            <span class="n">plt</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_3d_plot</span><span class="p">(</span><span class="n">label_stable</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">plt</span></div>

<div class="viewcode-block" id="PDPlotter.plot_element_profile"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.PDPlotter.plot_element_profile">[docs]</a>    <span class="k">def</span> <span class="nf">plot_element_profile</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">element</span><span class="p">,</span> <span class="n">comp</span><span class="p">,</span> <span class="n">show_label_index</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                             <span class="n">xlim</span><span class="o">=</span><span class="mi">5</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Draw the element profile plot for a composition varying different</span>
<span class="sd">        chemical potential of an element.</span>
<span class="sd">        X value is the negative value of the chemical potential reference to</span>
<span class="sd">        elemental chemical potential. For example, if choose Element(&quot;Li&quot;),</span>
<span class="sd">        X= -(µLi-µLi0), which corresponds to the voltage versus metal anode.</span>
<span class="sd">        Y values represent for the number of element uptake in this composition</span>
<span class="sd">        (unit: per atom). All reactions are printed to help choosing the</span>
<span class="sd">        profile steps you want to show label in the plot.</span>

<span class="sd">        Args:</span>
<span class="sd">         element (Element): An element of which the chemical potential is</span>
<span class="sd">            considered. It also must be in the phase diagram.</span>
<span class="sd">         comp (Composition): A composition.</span>
<span class="sd">         show_label_index (list of integers): The labels for reaction products</span>
<span class="sd">            you want to show in the plot. Default to None (not showing any</span>
<span class="sd">            annotation for reaction products). For the profile steps you want</span>
<span class="sd">            to show the labels, just add it to the show_label_index. The</span>
<span class="sd">            profile step counts from zero. For example, you can set</span>
<span class="sd">            show_label_index=[0, 2, 5] to label profile step 0,2,5.</span>
<span class="sd">         xlim (float): The max x value. x value is from 0 to xlim. Default to</span>
<span class="sd">            5 eV.</span>

<span class="sd">        Returns:</span>
<span class="sd">            Plot of element profile evolution by varying the chemical potential</span>
<span class="sd">            of an element.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">plt</span> <span class="o">=</span> <span class="n">pretty_plot</span><span class="p">(</span><span class="mi">12</span><span class="p">,</span> <span class="mi">8</span><span class="p">)</span>
        <span class="n">pd</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pd</span>
        <span class="n">evolution</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">get_element_profile</span><span class="p">(</span><span class="n">element</span><span class="p">,</span> <span class="n">comp</span><span class="p">)</span>
        <span class="n">num_atoms</span> <span class="o">=</span> <span class="n">evolution</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="s2">&quot;reaction&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">reactants</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">num_atoms</span>
        <span class="n">element_energy</span> <span class="o">=</span> <span class="n">evolution</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="s1">&#39;chempot&#39;</span><span class="p">]</span>
        <span class="n">x1</span><span class="p">,</span> <span class="n">x2</span><span class="p">,</span> <span class="n">y1</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="kc">None</span>
        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">d</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">evolution</span><span class="p">):</span>
            <span class="n">v</span> <span class="o">=</span> <span class="o">-</span><span class="p">(</span><span class="n">d</span><span class="p">[</span><span class="s2">&quot;chempot&quot;</span><span class="p">]</span> <span class="o">-</span> <span class="n">element_energy</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">i</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">([</span><span class="n">x2</span><span class="p">,</span> <span class="n">x2</span><span class="p">],</span> <span class="p">[</span><span class="n">y1</span><span class="p">,</span> <span class="n">d</span><span class="p">[</span><span class="s2">&quot;evolution&quot;</span><span class="p">]</span> <span class="o">/</span> <span class="n">num_atoms</span><span class="p">],</span>
                         <span class="s1">&#39;k&#39;</span><span class="p">,</span> <span class="n">linewidth</span><span class="o">=</span><span class="mf">2.5</span><span class="p">)</span>
            <span class="n">x1</span> <span class="o">=</span> <span class="n">v</span>
            <span class="n">y1</span> <span class="o">=</span> <span class="n">d</span><span class="p">[</span><span class="s2">&quot;evolution&quot;</span><span class="p">]</span> <span class="o">/</span> <span class="n">num_atoms</span>

            <span class="k">if</span> <span class="n">i</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="n">evolution</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">:</span>
                <span class="n">x2</span> <span class="o">=</span> <span class="o">-</span> <span class="p">(</span><span class="n">evolution</span><span class="p">[</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">][</span><span class="s2">&quot;chempot&quot;</span><span class="p">]</span> <span class="o">-</span> <span class="n">element_energy</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">x2</span> <span class="o">=</span> <span class="mf">5.0</span>
            <span class="k">if</span> <span class="n">show_label_index</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">show_label_index</span><span class="p">:</span>
                <span class="n">products</span> <span class="o">=</span> <span class="p">[</span><span class="n">re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="sa">r</span><span class="s2">&quot;(\d+)&quot;</span><span class="p">,</span> <span class="sa">r</span><span class="s2">&quot;$_{\1}$&quot;</span><span class="p">,</span> <span class="n">p</span><span class="o">.</span><span class="n">reduced_formula</span><span class="p">)</span>
                            <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">d</span><span class="p">[</span><span class="s2">&quot;reaction&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">products</span>
                            <span class="k">if</span> <span class="n">p</span><span class="o">.</span><span class="n">reduced_formula</span> <span class="o">!=</span> <span class="n">element</span><span class="o">.</span><span class="n">symbol</span><span class="p">]</span>
                <span class="n">plt</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span><span class="s2">&quot;, &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">products</span><span class="p">),</span> <span class="n">xy</span><span class="o">=</span><span class="p">(</span><span class="n">v</span> <span class="o">+</span> <span class="mf">0.05</span><span class="p">,</span> <span class="n">y1</span> <span class="o">+</span> <span class="mf">0.05</span><span class="p">),</span>
                             <span class="n">fontsize</span><span class="o">=</span><span class="mi">24</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="s1">&#39;r&#39;</span><span class="p">)</span>
                <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">([</span><span class="n">x1</span><span class="p">,</span> <span class="n">x2</span><span class="p">],</span> <span class="p">[</span><span class="n">y1</span><span class="p">,</span> <span class="n">y1</span><span class="p">],</span> <span class="s1">&#39;r&#39;</span><span class="p">,</span> <span class="n">linewidth</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">([</span><span class="n">x1</span><span class="p">,</span> <span class="n">x2</span><span class="p">],</span> <span class="p">[</span><span class="n">y1</span><span class="p">,</span> <span class="n">y1</span><span class="p">],</span> <span class="s1">&#39;k&#39;</span><span class="p">,</span> <span class="n">linewidth</span><span class="o">=</span><span class="mf">2.5</span><span class="p">)</span>

        <span class="n">plt</span><span class="o">.</span><span class="n">xlim</span><span class="p">((</span><span class="mi">0</span><span class="p">,</span> <span class="n">xlim</span><span class="p">))</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">xlabel</span><span class="p">(</span><span class="s2">&quot;-$</span><span class="se">\\</span><span class="s2">Delta{</span><span class="se">\\</span><span class="s2">mu}$ (eV)&quot;</span><span class="p">)</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">ylabel</span><span class="p">(</span><span class="s2">&quot;Uptake per atom&quot;</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">plt</span></div>

<div class="viewcode-block" id="PDPlotter.show"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.PDPlotter.show">[docs]</a>    <span class="k">def</span> <span class="nf">show</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sa">r</span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Draws the phase diagram using Matplotlib and show it.</span>

<span class="sd">        Args:</span>
<span class="sd">            *args: Passed to get_plot.</span>
<span class="sd">            **kwargs: Passed to get_plot.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">get_plot</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span><span class="o">.</span><span class="n">show</span><span class="p">()</span></div>

    <span class="k">def</span> <span class="nf">_get_2d_plot</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">label_stable</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">label_unstable</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
                     <span class="n">ordering</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">energy_colormap</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">vmin_mev</span><span class="o">=-</span><span class="mf">60.0</span><span class="p">,</span>
                     <span class="n">vmax_mev</span><span class="o">=</span><span class="mf">60.0</span><span class="p">,</span> <span class="n">show_colorbar</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
                     <span class="n">process_attributes</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">plt</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Shows the plot using pylab.  Usually I won&#39;t do imports in methods,</span>
<span class="sd">        but since plotting is a fairly expensive library to load and not all</span>
<span class="sd">        machines have matplotlib installed, I have done it this way.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">plt</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">plt</span> <span class="o">=</span> <span class="n">pretty_plot</span><span class="p">(</span><span class="mi">8</span><span class="p">,</span> <span class="mi">6</span><span class="p">)</span>
        <span class="kn">from</span> <span class="nn">matplotlib.font_manager</span> <span class="kn">import</span> <span class="n">FontProperties</span>
        <span class="k">if</span> <span class="n">ordering</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="p">(</span><span class="n">lines</span><span class="p">,</span> <span class="n">labels</span><span class="p">,</span> <span class="n">unstable</span><span class="p">)</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">pd_plot_data</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="p">(</span><span class="n">_lines</span><span class="p">,</span> <span class="n">_labels</span><span class="p">,</span> <span class="n">_unstable</span><span class="p">)</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">pd_plot_data</span>
            <span class="p">(</span><span class="n">lines</span><span class="p">,</span> <span class="n">labels</span><span class="p">,</span> <span class="n">unstable</span><span class="p">)</span> <span class="o">=</span> <span class="n">order_phase_diagram</span><span class="p">(</span>
                <span class="n">_lines</span><span class="p">,</span> <span class="n">_labels</span><span class="p">,</span> <span class="n">_unstable</span><span class="p">,</span> <span class="n">ordering</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">energy_colormap</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">process_attributes</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="ow">in</span> <span class="n">lines</span><span class="p">:</span>
                    <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="s2">&quot;k-&quot;</span><span class="p">,</span> <span class="n">linewidth</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">markeredgecolor</span><span class="o">=</span><span class="s2">&quot;k&quot;</span><span class="p">)</span>
                <span class="c1"># One should think about a clever way to have &quot;complex&quot;</span>
                <span class="c1"># attributes with complex processing options but with a clear</span>
                <span class="c1"># logic. At this moment, I just use the attributes to know</span>
                <span class="c1"># whether an entry is a new compound or an existing (from the</span>
                <span class="c1">#  ICSD or from the MP) one.</span>
                <span class="k">for</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="ow">in</span> <span class="n">labels</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
                    <span class="k">if</span> <span class="n">labels</span><span class="p">[(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)]</span><span class="o">.</span><span class="n">attribute</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> \
                            <span class="n">labels</span><span class="p">[(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)]</span><span class="o">.</span><span class="n">attribute</span> <span class="o">==</span> <span class="s2">&quot;existing&quot;</span><span class="p">:</span>
                        <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="s2">&quot;ko&quot;</span><span class="p">,</span> <span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">plotkwargs</span><span class="p">)</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="s2">&quot;k*&quot;</span><span class="p">,</span> <span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">plotkwargs</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="ow">in</span> <span class="n">lines</span><span class="p">:</span>
                    <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="s2">&quot;ko-&quot;</span><span class="p">,</span> <span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">plotkwargs</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="kn">from</span> <span class="nn">matplotlib.colors</span> <span class="kn">import</span> <span class="n">Normalize</span><span class="p">,</span> <span class="n">LinearSegmentedColormap</span>
            <span class="kn">from</span> <span class="nn">matplotlib.cm</span> <span class="kn">import</span> <span class="n">ScalarMappable</span>
            <span class="k">for</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="ow">in</span> <span class="n">lines</span><span class="p">:</span>
                <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="s2">&quot;k-&quot;</span><span class="p">,</span> <span class="n">markeredgecolor</span><span class="o">=</span><span class="s2">&quot;k&quot;</span><span class="p">)</span>
            <span class="n">vmin</span> <span class="o">=</span> <span class="n">vmin_mev</span> <span class="o">/</span> <span class="mf">1000.0</span>
            <span class="n">vmax</span> <span class="o">=</span> <span class="n">vmax_mev</span> <span class="o">/</span> <span class="mf">1000.0</span>
            <span class="k">if</span> <span class="n">energy_colormap</span> <span class="o">==</span> <span class="s1">&#39;default&#39;</span><span class="p">:</span>
                <span class="n">mid</span> <span class="o">=</span> <span class="o">-</span> <span class="n">vmin</span> <span class="o">/</span> <span class="p">(</span><span class="n">vmax</span> <span class="o">-</span> <span class="n">vmin</span><span class="p">)</span>
                <span class="n">cmap</span> <span class="o">=</span> <span class="n">LinearSegmentedColormap</span><span class="o">.</span><span class="n">from_list</span><span class="p">(</span>
                    <span class="s1">&#39;my_colormap&#39;</span><span class="p">,</span> <span class="p">[(</span><span class="mf">0.0</span><span class="p">,</span> <span class="s1">&#39;#005500&#39;</span><span class="p">),</span> <span class="p">(</span><span class="n">mid</span><span class="p">,</span> <span class="s1">&#39;#55FF55&#39;</span><span class="p">),</span>
                                    <span class="p">(</span><span class="n">mid</span><span class="p">,</span> <span class="s1">&#39;#FFAAAA&#39;</span><span class="p">),</span> <span class="p">(</span><span class="mf">1.0</span><span class="p">,</span> <span class="s1">&#39;#FF0000&#39;</span><span class="p">)])</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">cmap</span> <span class="o">=</span> <span class="n">energy_colormap</span>
            <span class="n">norm</span> <span class="o">=</span> <span class="n">Normalize</span><span class="p">(</span><span class="n">vmin</span><span class="o">=</span><span class="n">vmin</span><span class="p">,</span> <span class="n">vmax</span><span class="o">=</span><span class="n">vmax</span><span class="p">)</span>
            <span class="n">_map</span> <span class="o">=</span> <span class="n">ScalarMappable</span><span class="p">(</span><span class="n">norm</span><span class="o">=</span><span class="n">norm</span><span class="p">,</span> <span class="n">cmap</span><span class="o">=</span><span class="n">cmap</span><span class="p">)</span>
            <span class="n">_energies</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">_pd</span><span class="o">.</span><span class="n">get_equilibrium_reaction_energy</span><span class="p">(</span><span class="n">entry</span><span class="p">)</span>
                         <span class="k">for</span> <span class="n">coord</span><span class="p">,</span> <span class="n">entry</span> <span class="ow">in</span> <span class="n">labels</span><span class="o">.</span><span class="n">items</span><span class="p">()]</span>
            <span class="n">energies</span> <span class="o">=</span> <span class="p">[</span><span class="n">en</span> <span class="k">if</span> <span class="n">en</span> <span class="o">&lt;</span> <span class="mf">0.0</span> <span class="k">else</span> <span class="o">-</span><span class="mf">0.00000001</span> <span class="k">for</span> <span class="n">en</span> <span class="ow">in</span> <span class="n">_energies</span><span class="p">]</span>
            <span class="n">vals_stable</span> <span class="o">=</span> <span class="n">_map</span><span class="o">.</span><span class="n">to_rgba</span><span class="p">(</span><span class="n">energies</span><span class="p">)</span>
            <span class="n">ii</span> <span class="o">=</span> <span class="mi">0</span>
            <span class="k">if</span> <span class="n">process_attributes</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="ow">in</span> <span class="n">labels</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
                    <span class="k">if</span> <span class="n">labels</span><span class="p">[(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)]</span><span class="o">.</span><span class="n">attribute</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> \
                            <span class="n">labels</span><span class="p">[(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)]</span><span class="o">.</span><span class="n">attribute</span> <span class="o">==</span> <span class="s2">&quot;existing&quot;</span><span class="p">:</span>
                        <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="s2">&quot;o&quot;</span><span class="p">,</span> <span class="n">markerfacecolor</span><span class="o">=</span><span class="n">vals_stable</span><span class="p">[</span><span class="n">ii</span><span class="p">],</span>
                                 <span class="n">markersize</span><span class="o">=</span><span class="mi">12</span><span class="p">)</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="s2">&quot;*&quot;</span><span class="p">,</span> <span class="n">markerfacecolor</span><span class="o">=</span><span class="n">vals_stable</span><span class="p">[</span><span class="n">ii</span><span class="p">],</span>
                                 <span class="n">markersize</span><span class="o">=</span><span class="mi">18</span><span class="p">)</span>
                    <span class="n">ii</span> <span class="o">+=</span> <span class="mi">1</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="ow">in</span> <span class="n">labels</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
                    <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="s2">&quot;o&quot;</span><span class="p">,</span> <span class="n">markerfacecolor</span><span class="o">=</span><span class="n">vals_stable</span><span class="p">[</span><span class="n">ii</span><span class="p">],</span>
                             <span class="n">markersize</span><span class="o">=</span><span class="mi">15</span><span class="p">)</span>
                    <span class="n">ii</span> <span class="o">+=</span> <span class="mi">1</span>

        <span class="n">font</span> <span class="o">=</span> <span class="n">FontProperties</span><span class="p">()</span>
        <span class="n">font</span><span class="o">.</span><span class="n">set_weight</span><span class="p">(</span><span class="s2">&quot;bold&quot;</span><span class="p">)</span>
        <span class="n">font</span><span class="o">.</span><span class="n">set_size</span><span class="p">(</span><span class="mi">24</span><span class="p">)</span>

        <span class="c1"># Sets a nice layout depending on the type of PD. Also defines a</span>
        <span class="c1"># &quot;center&quot; for the PD, which then allows the annotations to be spread</span>
        <span class="c1"># out in a nice manner.</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_pd</span><span class="o">.</span><span class="n">elements</span><span class="p">)</span> <span class="o">==</span> <span class="mi">3</span><span class="p">:</span>
            <span class="n">plt</span><span class="o">.</span><span class="n">axis</span><span class="p">(</span><span class="s2">&quot;equal&quot;</span><span class="p">)</span>
            <span class="n">plt</span><span class="o">.</span><span class="n">xlim</span><span class="p">((</span><span class="o">-</span><span class="mf">0.1</span><span class="p">,</span> <span class="mf">1.2</span><span class="p">))</span>
            <span class="n">plt</span><span class="o">.</span><span class="n">ylim</span><span class="p">((</span><span class="o">-</span><span class="mf">0.1</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">))</span>
            <span class="n">plt</span><span class="o">.</span><span class="n">axis</span><span class="p">(</span><span class="s2">&quot;off&quot;</span><span class="p">)</span>
            <span class="n">center</span> <span class="o">=</span> <span class="p">(</span><span class="mf">0.5</span><span class="p">,</span> <span class="n">math</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span> <span class="o">/</span> <span class="mi">6</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">all_coords</span> <span class="o">=</span> <span class="n">labels</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span>
            <span class="n">miny</span> <span class="o">=</span> <span class="nb">min</span><span class="p">([</span><span class="n">c</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">all_coords</span><span class="p">])</span>
            <span class="n">ybuffer</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">miny</span><span class="p">)</span> <span class="o">*</span> <span class="mf">0.1</span><span class="p">,</span> <span class="mf">0.1</span><span class="p">)</span>
            <span class="n">plt</span><span class="o">.</span><span class="n">xlim</span><span class="p">((</span><span class="o">-</span><span class="mf">0.1</span><span class="p">,</span> <span class="mf">1.1</span><span class="p">))</span>
            <span class="n">plt</span><span class="o">.</span><span class="n">ylim</span><span class="p">((</span><span class="n">miny</span> <span class="o">-</span> <span class="n">ybuffer</span><span class="p">,</span> <span class="n">ybuffer</span><span class="p">))</span>
            <span class="n">center</span> <span class="o">=</span> <span class="p">(</span><span class="mf">0.5</span><span class="p">,</span> <span class="n">miny</span> <span class="o">/</span> <span class="mi">2</span><span class="p">)</span>
            <span class="n">plt</span><span class="o">.</span><span class="n">xlabel</span><span class="p">(</span><span class="s2">&quot;Fraction&quot;</span><span class="p">,</span> <span class="n">fontsize</span><span class="o">=</span><span class="mi">28</span><span class="p">,</span> <span class="n">fontweight</span><span class="o">=</span><span class="s1">&#39;bold&#39;</span><span class="p">)</span>
            <span class="n">plt</span><span class="o">.</span><span class="n">ylabel</span><span class="p">(</span><span class="s2">&quot;Formation energy (eV/atom)&quot;</span><span class="p">,</span> <span class="n">fontsize</span><span class="o">=</span><span class="mi">28</span><span class="p">,</span>
                       <span class="n">fontweight</span><span class="o">=</span><span class="s1">&#39;bold&#39;</span><span class="p">)</span>

        <span class="k">for</span> <span class="n">coords</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">labels</span><span class="o">.</span><span class="n">keys</span><span class="p">(),</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="o">-</span><span class="n">x</span><span class="p">[</span><span class="mi">1</span><span class="p">]):</span>
            <span class="n">entry</span> <span class="o">=</span> <span class="n">labels</span><span class="p">[</span><span class="n">coords</span><span class="p">]</span>
            <span class="n">label</span> <span class="o">=</span> <span class="n">entry</span><span class="o">.</span><span class="n">name</span>

            <span class="c1"># The follow defines an offset for the annotation text emanating</span>
            <span class="c1"># from the center of the PD. Results in fairly nice layouts for the</span>
            <span class="c1"># most part.</span>
            <span class="n">vec</span> <span class="o">=</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">coords</span><span class="p">)</span> <span class="o">-</span> <span class="n">center</span><span class="p">)</span>
            <span class="n">vec</span> <span class="o">=</span> <span class="n">vec</span> <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">vec</span><span class="p">)</span> <span class="o">*</span> <span class="mi">10</span> <span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">vec</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">0</span> \
                <span class="k">else</span> <span class="n">vec</span>
            <span class="n">valign</span> <span class="o">=</span> <span class="s2">&quot;bottom&quot;</span> <span class="k">if</span> <span class="n">vec</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="k">else</span> <span class="s2">&quot;top&quot;</span>
            <span class="k">if</span> <span class="n">vec</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&lt;</span> <span class="o">-</span><span class="mf">0.01</span><span class="p">:</span>
                <span class="n">halign</span> <span class="o">=</span> <span class="s2">&quot;right&quot;</span>
            <span class="k">elif</span> <span class="n">vec</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mf">0.01</span><span class="p">:</span>
                <span class="n">halign</span> <span class="o">=</span> <span class="s2">&quot;left&quot;</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">halign</span> <span class="o">=</span> <span class="s2">&quot;center&quot;</span>
            <span class="k">if</span> <span class="n">label_stable</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">process_attributes</span> <span class="ow">and</span> <span class="n">entry</span><span class="o">.</span><span class="n">attribute</span> <span class="o">==</span> <span class="s1">&#39;new&#39;</span><span class="p">:</span>
                    <span class="n">plt</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span><span class="n">latexify</span><span class="p">(</span><span class="n">label</span><span class="p">),</span> <span class="n">coords</span><span class="p">,</span> <span class="n">xytext</span><span class="o">=</span><span class="n">vec</span><span class="p">,</span>
                                 <span class="n">textcoords</span><span class="o">=</span><span class="s2">&quot;offset points&quot;</span><span class="p">,</span>
                                 <span class="n">horizontalalignment</span><span class="o">=</span><span class="n">halign</span><span class="p">,</span>
                                 <span class="n">verticalalignment</span><span class="o">=</span><span class="n">valign</span><span class="p">,</span>
                                 <span class="n">fontproperties</span><span class="o">=</span><span class="n">font</span><span class="p">,</span>
                                 <span class="n">color</span><span class="o">=</span><span class="s1">&#39;g&#39;</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">plt</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span><span class="n">latexify</span><span class="p">(</span><span class="n">label</span><span class="p">),</span> <span class="n">coords</span><span class="p">,</span> <span class="n">xytext</span><span class="o">=</span><span class="n">vec</span><span class="p">,</span>
                                 <span class="n">textcoords</span><span class="o">=</span><span class="s2">&quot;offset points&quot;</span><span class="p">,</span>
                                 <span class="n">horizontalalignment</span><span class="o">=</span><span class="n">halign</span><span class="p">,</span>
                                 <span class="n">verticalalignment</span><span class="o">=</span><span class="n">valign</span><span class="p">,</span>
                                 <span class="n">fontproperties</span><span class="o">=</span><span class="n">font</span><span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">show_unstable</span><span class="p">:</span>
            <span class="n">font</span> <span class="o">=</span> <span class="n">FontProperties</span><span class="p">()</span>
            <span class="n">font</span><span class="o">.</span><span class="n">set_size</span><span class="p">(</span><span class="mi">16</span><span class="p">)</span>
            <span class="n">energies_unstable</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">_pd</span><span class="o">.</span><span class="n">get_e_above_hull</span><span class="p">(</span><span class="n">entry</span><span class="p">)</span>
                                 <span class="k">for</span> <span class="n">entry</span><span class="p">,</span> <span class="n">coord</span> <span class="ow">in</span> <span class="n">unstable</span><span class="o">.</span><span class="n">items</span><span class="p">()]</span>
            <span class="k">if</span> <span class="n">energy_colormap</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">energies</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">energies_unstable</span><span class="p">)</span>
                <span class="n">vals_unstable</span> <span class="o">=</span> <span class="n">_map</span><span class="o">.</span><span class="n">to_rgba</span><span class="p">(</span><span class="n">energies_unstable</span><span class="p">)</span>
            <span class="n">ii</span> <span class="o">=</span> <span class="mi">0</span>
            <span class="k">for</span> <span class="n">entry</span><span class="p">,</span> <span class="n">coords</span> <span class="ow">in</span> <span class="n">unstable</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="n">ehull</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pd</span><span class="o">.</span><span class="n">get_e_above_hull</span><span class="p">(</span><span class="n">entry</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">ehull</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">show_unstable</span><span class="p">:</span>
                    <span class="n">vec</span> <span class="o">=</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">coords</span><span class="p">)</span> <span class="o">-</span> <span class="n">center</span><span class="p">)</span>
                    <span class="n">vec</span> <span class="o">=</span> <span class="n">vec</span> <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">vec</span><span class="p">)</span> <span class="o">*</span> <span class="mi">10</span> \
                        <span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">vec</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">0</span> <span class="k">else</span> <span class="n">vec</span>
                    <span class="n">label</span> <span class="o">=</span> <span class="n">entry</span><span class="o">.</span><span class="n">name</span>
                    <span class="k">if</span> <span class="n">energy_colormap</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                        <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">coords</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">coords</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="s2">&quot;ks&quot;</span><span class="p">,</span> <span class="n">linewidth</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span>
                                 <span class="n">markeredgecolor</span><span class="o">=</span><span class="s2">&quot;k&quot;</span><span class="p">,</span> <span class="n">markerfacecolor</span><span class="o">=</span><span class="s2">&quot;r&quot;</span><span class="p">,</span>
                                 <span class="n">markersize</span><span class="o">=</span><span class="mi">8</span><span class="p">)</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">coords</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">coords</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="s2">&quot;s&quot;</span><span class="p">,</span> <span class="n">linewidth</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span>
                                 <span class="n">markeredgecolor</span><span class="o">=</span><span class="s2">&quot;k&quot;</span><span class="p">,</span>
                                 <span class="n">markerfacecolor</span><span class="o">=</span><span class="n">vals_unstable</span><span class="p">[</span><span class="n">ii</span><span class="p">],</span>
                                 <span class="n">markersize</span><span class="o">=</span><span class="mi">8</span><span class="p">)</span>
                    <span class="k">if</span> <span class="n">label_unstable</span><span class="p">:</span>
                        <span class="n">plt</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span><span class="n">latexify</span><span class="p">(</span><span class="n">label</span><span class="p">),</span> <span class="n">coords</span><span class="p">,</span> <span class="n">xytext</span><span class="o">=</span><span class="n">vec</span><span class="p">,</span>
                                     <span class="n">textcoords</span><span class="o">=</span><span class="s2">&quot;offset points&quot;</span><span class="p">,</span>
                                     <span class="n">horizontalalignment</span><span class="o">=</span><span class="n">halign</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="s2">&quot;b&quot;</span><span class="p">,</span>
                                     <span class="n">verticalalignment</span><span class="o">=</span><span class="n">valign</span><span class="p">,</span>
                                     <span class="n">fontproperties</span><span class="o">=</span><span class="n">font</span><span class="p">)</span>
                    <span class="n">ii</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="k">if</span> <span class="n">energy_colormap</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">show_colorbar</span><span class="p">:</span>
            <span class="n">_map</span><span class="o">.</span><span class="n">set_array</span><span class="p">(</span><span class="n">energies</span><span class="p">)</span>
            <span class="n">cbar</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">colorbar</span><span class="p">(</span><span class="n">_map</span><span class="p">)</span>
            <span class="n">cbar</span><span class="o">.</span><span class="n">set_label</span><span class="p">(</span>
                <span class="s1">&#39;Energy [meV/at] above hull (in red)</span><span class="se">\n</span><span class="s1">Inverse energy [&#39;</span>
                <span class="s1">&#39;meV/at] above hull (in green)&#39;</span><span class="p">,</span>
                <span class="n">rotation</span><span class="o">=-</span><span class="mi">90</span><span class="p">,</span> <span class="n">ha</span><span class="o">=</span><span class="s1">&#39;left&#39;</span><span class="p">,</span> <span class="n">va</span><span class="o">=</span><span class="s1">&#39;center&#39;</span><span class="p">)</span>
        <span class="n">f</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">gcf</span><span class="p">()</span>
        <span class="n">f</span><span class="o">.</span><span class="n">set_size_inches</span><span class="p">((</span><span class="mi">8</span><span class="p">,</span> <span class="mi">6</span><span class="p">))</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">subplots_adjust</span><span class="p">(</span><span class="n">left</span><span class="o">=</span><span class="mf">0.09</span><span class="p">,</span> <span class="n">right</span><span class="o">=</span><span class="mf">0.98</span><span class="p">,</span> <span class="n">top</span><span class="o">=</span><span class="mf">0.98</span><span class="p">,</span> <span class="n">bottom</span><span class="o">=</span><span class="mf">0.07</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">plt</span>

    <span class="k">def</span> <span class="nf">_get_3d_plot</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">label_stable</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Shows the plot using pylab.  Usually I won&quot;t do imports in methods,</span>
<span class="sd">        but since plotting is a fairly expensive library to load and not all</span>
<span class="sd">        machines have matplotlib installed, I have done it this way.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span>
        <span class="kn">import</span> <span class="nn">mpl_toolkits.mplot3d.axes3d</span> <span class="k">as</span> <span class="nn">p3</span>
        <span class="kn">from</span> <span class="nn">matplotlib.font_manager</span> <span class="kn">import</span> <span class="n">FontProperties</span>
        <span class="n">fig</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">()</span>
        <span class="n">ax</span> <span class="o">=</span> <span class="n">p3</span><span class="o">.</span><span class="n">Axes3D</span><span class="p">(</span><span class="n">fig</span><span class="p">)</span>
        <span class="n">font</span> <span class="o">=</span> <span class="n">FontProperties</span><span class="p">()</span>
        <span class="n">font</span><span class="o">.</span><span class="n">set_weight</span><span class="p">(</span><span class="s2">&quot;bold&quot;</span><span class="p">)</span>
        <span class="n">font</span><span class="o">.</span><span class="n">set_size</span><span class="p">(</span><span class="mi">20</span><span class="p">)</span>
        <span class="p">(</span><span class="n">lines</span><span class="p">,</span> <span class="n">labels</span><span class="p">,</span> <span class="n">unstable</span><span class="p">)</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">pd_plot_data</span>
        <span class="n">count</span> <span class="o">=</span> <span class="mi">1</span>
        <span class="n">newlabels</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span> <span class="ow">in</span> <span class="n">lines</span><span class="p">:</span>
            <span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">,</span> <span class="s2">&quot;bo-&quot;</span><span class="p">,</span> <span class="n">linewidth</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">markeredgecolor</span><span class="o">=</span><span class="s2">&quot;b&quot;</span><span class="p">,</span>
                    <span class="n">markerfacecolor</span><span class="o">=</span><span class="s2">&quot;r&quot;</span><span class="p">,</span> <span class="n">markersize</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">coords</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">labels</span><span class="o">.</span><span class="n">keys</span><span class="p">()):</span>
            <span class="n">entry</span> <span class="o">=</span> <span class="n">labels</span><span class="p">[</span><span class="n">coords</span><span class="p">]</span>
            <span class="n">label</span> <span class="o">=</span> <span class="n">entry</span><span class="o">.</span><span class="n">name</span>
            <span class="k">if</span> <span class="n">label_stable</span><span class="p">:</span>
                <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">entry</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">elements</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
                    <span class="n">ax</span><span class="o">.</span><span class="n">text</span><span class="p">(</span><span class="n">coords</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">coords</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">coords</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="n">label</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">ax</span><span class="o">.</span><span class="n">text</span><span class="p">(</span><span class="n">coords</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">coords</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">coords</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="nb">str</span><span class="p">(</span><span class="n">count</span><span class="p">))</span>
                    <span class="n">newlabels</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">{}</span><span class="s2"> : </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">count</span><span class="p">,</span> <span class="n">latexify</span><span class="p">(</span><span class="n">label</span><span class="p">)))</span>
                    <span class="n">count</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">figtext</span><span class="p">(</span><span class="mf">0.01</span><span class="p">,</span> <span class="mf">0.01</span><span class="p">,</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">newlabels</span><span class="p">))</span>
        <span class="n">ax</span><span class="o">.</span><span class="n">axis</span><span class="p">(</span><span class="s2">&quot;off&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">plt</span>

<div class="viewcode-block" id="PDPlotter.write_image"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.PDPlotter.write_image">[docs]</a>    <span class="k">def</span> <span class="nf">write_image</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">stream</span><span class="p">,</span> <span class="n">image_format</span><span class="o">=</span><span class="s2">&quot;svg&quot;</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sa">r</span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Writes the phase diagram to an image in a stream.</span>

<span class="sd">        Args:</span>
<span class="sd">            stream:</span>
<span class="sd">                stream to write to. Can be a file stream or a StringIO stream.</span>
<span class="sd">            image_format</span>
<span class="sd">                format for image. Can be any of matplotlib supported formats.</span>
<span class="sd">                Defaults to svg for best results for vector graphics.</span>
<span class="sd">            **kwargs: Pass through to get_plot functino.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">plt</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_plot</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

        <span class="n">f</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">gcf</span><span class="p">()</span>
        <span class="n">f</span><span class="o">.</span><span class="n">set_size_inches</span><span class="p">((</span><span class="mi">12</span><span class="p">,</span> <span class="mi">10</span><span class="p">))</span>

        <span class="n">plt</span><span class="o">.</span><span class="n">savefig</span><span class="p">(</span><span class="n">stream</span><span class="p">,</span> <span class="nb">format</span><span class="o">=</span><span class="n">image_format</span><span class="p">)</span></div>

<div class="viewcode-block" id="PDPlotter.plot_chempot_range_map"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.PDPlotter.plot_chempot_range_map">[docs]</a>    <span class="k">def</span> <span class="nf">plot_chempot_range_map</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">elements</span><span class="p">,</span> <span class="n">referenced</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Plot the chemical potential range _map. Currently works only for</span>
<span class="sd">        3-component PDs.</span>

<span class="sd">        Args:</span>
<span class="sd">            elements: Sequence of elements to be considered as independent</span>
<span class="sd">                variables. E.g., if you want to show the stability ranges of</span>
<span class="sd">                all Li-Co-O phases wrt to uLi and uO, you will supply</span>
<span class="sd">                [Element(&quot;Li&quot;), Element(&quot;O&quot;)]</span>
<span class="sd">            referenced: if True, gives the results with a reference being the</span>
<span class="sd">                        energy of the elemental phase. If False, gives absolute values.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">get_chempot_range_map_plot</span><span class="p">(</span><span class="n">elements</span><span class="p">,</span> <span class="n">referenced</span><span class="o">=</span><span class="n">referenced</span><span class="p">)</span><span class="o">.</span><span class="n">show</span><span class="p">()</span></div>

<div class="viewcode-block" id="PDPlotter.get_chempot_range_map_plot"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.PDPlotter.get_chempot_range_map_plot">[docs]</a>    <span class="k">def</span> <span class="nf">get_chempot_range_map_plot</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">elements</span><span class="p">,</span> <span class="n">referenced</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns a plot of the chemical potential range _map. Currently works</span>
<span class="sd">        only for 3-component PDs.</span>

<span class="sd">        Args:</span>
<span class="sd">            elements: Sequence of elements to be considered as independent</span>
<span class="sd">                variables. E.g., if you want to show the stability ranges of</span>
<span class="sd">                all Li-Co-O phases wrt to uLi and uO, you will supply</span>
<span class="sd">                [Element(&quot;Li&quot;), Element(&quot;O&quot;)]</span>
<span class="sd">            referenced: if True, gives the results with a reference being the</span>
<span class="sd">                        energy of the elemental phase. If False, gives absolute values.</span>

<span class="sd">        Returns:</span>
<span class="sd">            A matplotlib plot object.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">plt</span> <span class="o">=</span> <span class="n">pretty_plot</span><span class="p">(</span><span class="mi">12</span><span class="p">,</span> <span class="mi">8</span><span class="p">)</span>
        <span class="n">chempot_ranges</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pd</span><span class="o">.</span><span class="n">get_chempot_range_map</span><span class="p">(</span>
            <span class="n">elements</span><span class="p">,</span> <span class="n">referenced</span><span class="o">=</span><span class="n">referenced</span><span class="p">)</span>
        <span class="n">missing_lines</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="n">excluded_region</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">entry</span><span class="p">,</span> <span class="n">lines</span> <span class="ow">in</span> <span class="n">chempot_ranges</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="n">comp</span> <span class="o">=</span> <span class="n">entry</span><span class="o">.</span><span class="n">composition</span>
            <span class="n">center_x</span> <span class="o">=</span> <span class="mi">0</span>
            <span class="n">center_y</span> <span class="o">=</span> <span class="mi">0</span>
            <span class="n">coords</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="n">contain_zero</span> <span class="o">=</span> <span class="nb">any</span><span class="p">([</span><span class="n">comp</span><span class="o">.</span><span class="n">get_atomic_fraction</span><span class="p">(</span><span class="n">el</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span>
                                <span class="k">for</span> <span class="n">el</span> <span class="ow">in</span> <span class="n">elements</span><span class="p">])</span>
            <span class="n">is_boundary</span> <span class="o">=</span> <span class="p">(</span><span class="ow">not</span> <span class="n">contain_zero</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">sum</span><span class="p">([</span><span class="n">comp</span><span class="o">.</span><span class="n">get_atomic_fraction</span><span class="p">(</span><span class="n">el</span><span class="p">)</span> <span class="k">for</span> <span class="n">el</span> <span class="ow">in</span> <span class="n">elements</span><span class="p">])</span> <span class="o">==</span> <span class="mi">1</span>
            <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">lines</span><span class="p">:</span>
                <span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">coords</span><span class="o">.</span><span class="n">transpose</span><span class="p">()</span>
                <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="s2">&quot;k-&quot;</span><span class="p">)</span>

                <span class="k">for</span> <span class="n">coord</span> <span class="ow">in</span> <span class="n">line</span><span class="o">.</span><span class="n">coords</span><span class="p">:</span>
                    <span class="k">if</span> <span class="ow">not</span> <span class="n">in_coord_list</span><span class="p">(</span><span class="n">coords</span><span class="p">,</span> <span class="n">coord</span><span class="p">):</span>
                        <span class="n">coords</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">coord</span><span class="o">.</span><span class="n">tolist</span><span class="p">())</span>
                        <span class="n">center_x</span> <span class="o">+=</span> <span class="n">coord</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
                        <span class="n">center_y</span> <span class="o">+=</span> <span class="n">coord</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
                <span class="k">if</span> <span class="n">is_boundary</span><span class="p">:</span>
                    <span class="n">excluded_region</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">line</span><span class="o">.</span><span class="n">coords</span><span class="p">)</span>

            <span class="k">if</span> <span class="n">coords</span> <span class="ow">and</span> <span class="n">contain_zero</span><span class="p">:</span>
                <span class="n">missing_lines</span><span class="p">[</span><span class="n">entry</span><span class="p">]</span> <span class="o">=</span> <span class="n">coords</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">xy</span> <span class="o">=</span> <span class="p">(</span><span class="n">center_x</span> <span class="o">/</span> <span class="nb">len</span><span class="p">(</span><span class="n">coords</span><span class="p">),</span> <span class="n">center_y</span> <span class="o">/</span> <span class="nb">len</span><span class="p">(</span><span class="n">coords</span><span class="p">))</span>
                <span class="n">plt</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span><span class="n">latexify</span><span class="p">(</span><span class="n">entry</span><span class="o">.</span><span class="n">name</span><span class="p">),</span> <span class="n">xy</span><span class="p">,</span> <span class="n">fontsize</span><span class="o">=</span><span class="mi">22</span><span class="p">)</span>

        <span class="n">ax</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">gca</span><span class="p">()</span>
        <span class="n">xlim</span> <span class="o">=</span> <span class="n">ax</span><span class="o">.</span><span class="n">get_xlim</span><span class="p">()</span>
        <span class="n">ylim</span> <span class="o">=</span> <span class="n">ax</span><span class="o">.</span><span class="n">get_ylim</span><span class="p">()</span>

        <span class="c1"># Shade the forbidden chemical potential regions.</span>
        <span class="n">excluded_region</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="n">xlim</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">ylim</span><span class="p">[</span><span class="mi">1</span><span class="p">]])</span>
        <span class="n">excluded_region</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">excluded_region</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">c</span><span class="p">:</span> <span class="n">c</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
        <span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">transpose</span><span class="p">(</span><span class="n">excluded_region</span><span class="p">)</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">fill</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="s2">&quot;0.80&quot;</span><span class="p">)</span>

        <span class="c1"># The hull does not generate the missing horizontal and vertical lines.</span>
        <span class="c1"># The following code fixes this.</span>
        <span class="n">el0</span> <span class="o">=</span> <span class="n">elements</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">el1</span> <span class="o">=</span> <span class="n">elements</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
        <span class="k">for</span> <span class="n">entry</span><span class="p">,</span> <span class="n">coords</span> <span class="ow">in</span> <span class="n">missing_lines</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="n">center_x</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">([</span><span class="n">c</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">coords</span><span class="p">])</span>
            <span class="n">center_y</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">([</span><span class="n">c</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">coords</span><span class="p">])</span>
            <span class="n">comp</span> <span class="o">=</span> <span class="n">entry</span><span class="o">.</span><span class="n">composition</span>
            <span class="n">is_x</span> <span class="o">=</span> <span class="n">comp</span><span class="o">.</span><span class="n">get_atomic_fraction</span><span class="p">(</span><span class="n">el0</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mf">0.01</span>
            <span class="n">is_y</span> <span class="o">=</span> <span class="n">comp</span><span class="o">.</span><span class="n">get_atomic_fraction</span><span class="p">(</span><span class="n">el1</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mf">0.01</span>
            <span class="n">n</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">coords</span><span class="p">)</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="p">(</span><span class="n">is_x</span> <span class="ow">and</span> <span class="n">is_y</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">is_x</span><span class="p">:</span>
                    <span class="n">coords</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">coords</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">c</span><span class="p">:</span> <span class="n">c</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
                    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">]:</span>
                        <span class="n">x</span> <span class="o">=</span> <span class="p">[</span><span class="nb">min</span><span class="p">(</span><span class="n">xlim</span><span class="p">),</span> <span class="n">coords</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">0</span><span class="p">]]</span>
                        <span class="n">y</span> <span class="o">=</span> <span class="p">[</span><span class="n">coords</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">1</span><span class="p">],</span> <span class="n">coords</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">1</span><span class="p">]]</span>
                        <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="s2">&quot;k&quot;</span><span class="p">)</span>
                        <span class="n">center_x</span> <span class="o">+=</span> <span class="nb">min</span><span class="p">(</span><span class="n">xlim</span><span class="p">)</span>
                        <span class="n">center_y</span> <span class="o">+=</span> <span class="n">coords</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span>
                <span class="k">elif</span> <span class="n">is_y</span><span class="p">:</span>
                    <span class="n">coords</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">coords</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">c</span><span class="p">:</span> <span class="n">c</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
                    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">]:</span>
                        <span class="n">x</span> <span class="o">=</span> <span class="p">[</span><span class="n">coords</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">0</span><span class="p">],</span> <span class="n">coords</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">0</span><span class="p">]]</span>
                        <span class="n">y</span> <span class="o">=</span> <span class="p">[</span><span class="n">coords</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">1</span><span class="p">],</span> <span class="nb">min</span><span class="p">(</span><span class="n">ylim</span><span class="p">)]</span>
                        <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="s2">&quot;k&quot;</span><span class="p">)</span>
                        <span class="n">center_x</span> <span class="o">+=</span> <span class="n">coords</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span>
                        <span class="n">center_y</span> <span class="o">+=</span> <span class="nb">min</span><span class="p">(</span><span class="n">ylim</span><span class="p">)</span>
                <span class="n">xy</span> <span class="o">=</span> <span class="p">(</span><span class="n">center_x</span> <span class="o">/</span> <span class="p">(</span><span class="n">n</span> <span class="o">+</span> <span class="mi">2</span><span class="p">),</span> <span class="n">center_y</span> <span class="o">/</span> <span class="p">(</span><span class="n">n</span> <span class="o">+</span> <span class="mi">2</span><span class="p">))</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">center_x</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">(</span><span class="n">coord</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">coord</span> <span class="ow">in</span> <span class="n">coords</span><span class="p">)</span> <span class="o">+</span> <span class="n">xlim</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
                <span class="n">center_y</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">(</span><span class="n">coord</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="k">for</span> <span class="n">coord</span> <span class="ow">in</span> <span class="n">coords</span><span class="p">)</span> <span class="o">+</span> <span class="n">ylim</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
                <span class="n">xy</span> <span class="o">=</span> <span class="p">(</span><span class="n">center_x</span> <span class="o">/</span> <span class="p">(</span><span class="n">n</span> <span class="o">+</span> <span class="mi">1</span><span class="p">),</span> <span class="n">center_y</span> <span class="o">/</span> <span class="p">(</span><span class="n">n</span> <span class="o">+</span> <span class="mi">1</span><span class="p">))</span>

            <span class="n">plt</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span><span class="n">latexify</span><span class="p">(</span><span class="n">entry</span><span class="o">.</span><span class="n">name</span><span class="p">),</span> <span class="n">xy</span><span class="p">,</span>
                         <span class="n">horizontalalignment</span><span class="o">=</span><span class="s2">&quot;center&quot;</span><span class="p">,</span>
                         <span class="n">verticalalignment</span><span class="o">=</span><span class="s2">&quot;center&quot;</span><span class="p">,</span> <span class="n">fontsize</span><span class="o">=</span><span class="mi">22</span><span class="p">)</span>

        <span class="n">plt</span><span class="o">.</span><span class="n">xlabel</span><span class="p">(</span><span class="s2">&quot;$</span><span class="se">\\</span><span class="s2">mu_{{</span><span class="si">{0}</span><span class="s2">}} - </span><span class="se">\\</span><span class="s2">mu_{{</span><span class="si">{0}</span><span class="s2">}}^0$ (eV)&quot;</span>
                   <span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">el0</span><span class="o">.</span><span class="n">symbol</span><span class="p">))</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">ylabel</span><span class="p">(</span><span class="s2">&quot;$</span><span class="se">\\</span><span class="s2">mu_{{</span><span class="si">{0}</span><span class="s2">}} - </span><span class="se">\\</span><span class="s2">mu_{{</span><span class="si">{0}</span><span class="s2">}}^0$ (eV)&quot;</span>
                   <span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">el1</span><span class="o">.</span><span class="n">symbol</span><span class="p">))</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">tight_layout</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">plt</span></div>

<div class="viewcode-block" id="PDPlotter.get_contour_pd_plot"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.PDPlotter.get_contour_pd_plot">[docs]</a>    <span class="k">def</span> <span class="nf">get_contour_pd_plot</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Plot a contour phase diagram plot, where phase triangles are colored</span>
<span class="sd">        according to degree of instability by interpolation. Currently only</span>
<span class="sd">        works for 3-component phase diagrams.</span>

<span class="sd">        Returns:</span>
<span class="sd">            A matplotlib plot object.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="kn">from</span> <span class="nn">scipy</span> <span class="kn">import</span> <span class="n">interpolate</span>
        <span class="kn">from</span> <span class="nn">matplotlib</span> <span class="kn">import</span> <span class="n">cm</span>

        <span class="n">pd</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pd</span>
        <span class="n">entries</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">qhull_entries</span>
        <span class="n">data</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">qhull_data</span><span class="p">)</span>

        <span class="n">plt</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_2d_plot</span><span class="p">()</span>
        <span class="n">data</span><span class="p">[:,</span> <span class="mi">0</span><span class="p">:</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">triangular_coord</span><span class="p">(</span><span class="n">data</span><span class="p">[:,</span> <span class="mi">0</span><span class="p">:</span><span class="mi">2</span><span class="p">])</span><span class="o">.</span><span class="n">transpose</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">e</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">entries</span><span class="p">):</span>
            <span class="n">data</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pd</span><span class="o">.</span><span class="n">get_e_above_hull</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>

        <span class="n">gridsize</span> <span class="o">=</span> <span class="mf">0.005</span>
        <span class="n">xnew</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mf">1.</span><span class="p">,</span> <span class="n">gridsize</span><span class="p">)</span>
        <span class="n">ynew</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">gridsize</span><span class="p">)</span>

        <span class="n">f</span> <span class="o">=</span> <span class="n">interpolate</span><span class="o">.</span><span class="n">LinearNDInterpolator</span><span class="p">(</span><span class="n">data</span><span class="p">[:,</span> <span class="mi">0</span><span class="p">:</span><span class="mi">2</span><span class="p">],</span> <span class="n">data</span><span class="p">[:,</span> <span class="mi">2</span><span class="p">])</span>
        <span class="n">znew</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="nb">len</span><span class="p">(</span><span class="n">ynew</span><span class="p">),</span> <span class="nb">len</span><span class="p">(</span><span class="n">xnew</span><span class="p">)))</span>
        <span class="k">for</span> <span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">xval</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">xnew</span><span class="p">):</span>
            <span class="k">for</span> <span class="p">(</span><span class="n">j</span><span class="p">,</span> <span class="n">yval</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">ynew</span><span class="p">):</span>
                <span class="n">znew</span><span class="p">[</span><span class="n">j</span><span class="p">,</span> <span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">f</span><span class="p">(</span><span class="n">xval</span><span class="p">,</span> <span class="n">yval</span><span class="p">)</span>

        <span class="n">plt</span><span class="o">.</span><span class="n">contourf</span><span class="p">(</span><span class="n">xnew</span><span class="p">,</span> <span class="n">ynew</span><span class="p">,</span> <span class="n">znew</span><span class="p">,</span> <span class="mi">1000</span><span class="p">,</span> <span class="n">cmap</span><span class="o">=</span><span class="n">cm</span><span class="o">.</span><span class="n">autumn_r</span><span class="p">)</span>

        <span class="n">plt</span><span class="o">.</span><span class="n">colorbar</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">plt</span></div></div>


<div class="viewcode-block" id="uniquelines"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.uniquelines">[docs]</a><span class="k">def</span> <span class="nf">uniquelines</span><span class="p">(</span><span class="n">q</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Given all the facets, convert it into a set of unique lines.  Specifically</span>
<span class="sd">    used for converting convex hull facets into line pairs of coordinates.</span>

<span class="sd">    Args:</span>
<span class="sd">        q: A 2-dim sequence, where each row represents a facet. E.g.,</span>
<span class="sd">            [[1,2,3],[3,6,7],...]</span>

<span class="sd">    Returns:</span>
<span class="sd">        setoflines:</span>
<span class="sd">            A set of tuple of lines.  E.g., ((1,2), (1,3), (2,3), ....)</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">setoflines</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
    <span class="k">for</span> <span class="n">facets</span> <span class="ow">in</span> <span class="n">q</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">itertools</span><span class="o">.</span><span class="n">combinations</span><span class="p">(</span><span class="n">facets</span><span class="p">,</span> <span class="mi">2</span><span class="p">):</span>
            <span class="n">setoflines</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="nb">tuple</span><span class="p">(</span><span class="nb">sorted</span><span class="p">(</span><span class="n">line</span><span class="p">)))</span>
    <span class="k">return</span> <span class="n">setoflines</span></div>


<div class="viewcode-block" id="triangular_coord"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.triangular_coord">[docs]</a><span class="k">def</span> <span class="nf">triangular_coord</span><span class="p">(</span><span class="n">coord</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Convert a 2D coordinate into a triangle-based coordinate system for a</span>
<span class="sd">    prettier phase diagram.</span>

<span class="sd">    Args:</span>
<span class="sd">        coordinate: coordinate used in the convex hull computation.</span>

<span class="sd">    Returns:</span>
<span class="sd">        coordinates in a triangular-based coordinate system.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">unitvec</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mf">0.5</span><span class="p">,</span> <span class="n">math</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span><span class="p">]])</span>
    <span class="n">result</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">coord</span><span class="p">),</span> <span class="n">unitvec</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">result</span><span class="o">.</span><span class="n">transpose</span><span class="p">()</span></div>


<div class="viewcode-block" id="tet_coord"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.tet_coord">[docs]</a><span class="k">def</span> <span class="nf">tet_coord</span><span class="p">(</span><span class="n">coord</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Convert a 3D coordinate into a tetrahedron based coordinate system for a</span>
<span class="sd">    prettier phase diagram.</span>

<span class="sd">    Args:</span>
<span class="sd">        coordinate: coordinate used in the convex hull computation.</span>

<span class="sd">    Returns:</span>
<span class="sd">        coordinates in a tetrahedron-based coordinate system.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">unitvec</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mf">0.5</span><span class="p">,</span> <span class="n">math</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span>
                        <span class="p">[</span><span class="mf">0.5</span><span class="p">,</span> <span class="mf">1.0</span> <span class="o">/</span> <span class="mf">3.0</span> <span class="o">*</span> <span class="n">math</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span><span class="p">,</span> <span class="n">math</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span> <span class="o">/</span> <span class="mi">3</span><span class="p">]])</span>
    <span class="n">result</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">coord</span><span class="p">),</span> <span class="n">unitvec</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">result</span><span class="o">.</span><span class="n">transpose</span><span class="p">()</span></div>


<div class="viewcode-block" id="order_phase_diagram"><a class="viewcode-back" href="../../../pymatgen.analysis.phase_diagram.html#pymatgen.analysis.phase_diagram.order_phase_diagram">[docs]</a><span class="k">def</span> <span class="nf">order_phase_diagram</span><span class="p">(</span><span class="n">lines</span><span class="p">,</span> <span class="n">stable_entries</span><span class="p">,</span> <span class="n">unstable_entries</span><span class="p">,</span> <span class="n">ordering</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Orders the entries (their coordinates) in a phase diagram plot according</span>
<span class="sd">    to the user specified ordering.</span>
<span class="sd">    Ordering should be given as [&#39;Up&#39;, &#39;Left&#39;, &#39;Right&#39;], where Up,</span>
<span class="sd">    Left and Right are the names of the entries in the upper, left and right</span>
<span class="sd">    corners of the triangle respectively.</span>

<span class="sd">    Args:</span>
<span class="sd">        lines: list of list of coordinates for lines in the PD.</span>
<span class="sd">        stable_entries: {coordinate : entry} for each stable node in the</span>
<span class="sd">            phase diagram. (Each coordinate can only have one stable phase)</span>
<span class="sd">        unstable_entries: {entry: coordinates} for all unstable nodes in the</span>
<span class="sd">            phase diagram.</span>
<span class="sd">        ordering: Ordering of the phase diagram, given as a list [&#39;Up&#39;,</span>
<span class="sd">            &#39;Left&#39;,&#39;Right&#39;]</span>

<span class="sd">    Returns:</span>
<span class="sd">        (newlines, newstable_entries, newunstable_entries):</span>
<span class="sd">        - newlines is a list of list of coordinates for lines in the PD.</span>
<span class="sd">        - newstable_entries is a {coordinate : entry} for each stable node</span>
<span class="sd">        in the phase diagram. (Each coordinate can only have one</span>
<span class="sd">        stable phase)</span>
<span class="sd">        - newunstable_entries is a {entry: coordinates} for all unstable</span>
<span class="sd">        nodes in the phase diagram.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">yup</span> <span class="o">=</span> <span class="o">-</span><span class="mf">1000.0</span>
    <span class="n">xleft</span> <span class="o">=</span> <span class="mf">1000.0</span>
    <span class="n">xright</span> <span class="o">=</span> <span class="o">-</span><span class="mf">1000.0</span>

    <span class="k">for</span> <span class="n">coord</span> <span class="ow">in</span> <span class="n">stable_entries</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">coord</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&gt;</span> <span class="n">xright</span><span class="p">:</span>
            <span class="n">xright</span> <span class="o">=</span> <span class="n">coord</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
            <span class="n">nameright</span> <span class="o">=</span> <span class="n">stable_entries</span><span class="p">[</span><span class="n">coord</span><span class="p">]</span><span class="o">.</span><span class="n">name</span>
        <span class="k">if</span> <span class="n">coord</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&lt;</span> <span class="n">xleft</span><span class="p">:</span>
            <span class="n">xleft</span> <span class="o">=</span> <span class="n">coord</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
            <span class="n">nameleft</span> <span class="o">=</span> <span class="n">stable_entries</span><span class="p">[</span><span class="n">coord</span><span class="p">]</span><span class="o">.</span><span class="n">name</span>
        <span class="k">if</span> <span class="n">coord</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">&gt;</span> <span class="n">yup</span><span class="p">:</span>
            <span class="n">yup</span> <span class="o">=</span> <span class="n">coord</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
            <span class="n">nameup</span> <span class="o">=</span> <span class="n">stable_entries</span><span class="p">[</span><span class="n">coord</span><span class="p">]</span><span class="o">.</span><span class="n">name</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">nameup</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">ordering</span><span class="p">)</span> <span class="ow">or</span> <span class="p">(</span><span class="n">nameright</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">ordering</span><span class="p">)</span> <span class="ow">or</span> <span class="p">(</span><span class="n">nameleft</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">ordering</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
            <span class="s1">&#39;Error in ordering_phase_diagram : </span><span class="se">\n</span><span class="s1">&quot;</span><span class="si">{up}</span><span class="s1">&quot;, &quot;</span><span class="si">{left}</span><span class="s1">&quot; and &quot;{&#39;</span>
            <span class="s1">&#39;right}&quot;&#39;</span>
            <span class="s1">&#39; should be in ordering : </span><span class="si">{ord}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">up</span><span class="o">=</span><span class="n">nameup</span><span class="p">,</span> <span class="n">left</span><span class="o">=</span><span class="n">nameleft</span><span class="p">,</span>
                                                    <span class="n">right</span><span class="o">=</span><span class="n">nameright</span><span class="p">,</span>
                                                    <span class="nb">ord</span><span class="o">=</span><span class="n">ordering</span><span class="p">))</span>

    <span class="n">cc</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mf">0.5</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="mf">3.0</span><span class="p">)</span> <span class="o">/</span> <span class="mf">6.0</span><span class="p">],</span> <span class="n">np</span><span class="o">.</span><span class="n">float</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">nameup</span> <span class="o">==</span> <span class="n">ordering</span><span class="p">[</span><span class="mi">0</span><span class="p">]:</span>
        <span class="k">if</span> <span class="n">nameleft</span> <span class="o">==</span> <span class="n">ordering</span><span class="p">[</span><span class="mi">1</span><span class="p">]:</span>
            <span class="c1"># The coordinates were already in the user ordering</span>
            <span class="k">return</span> <span class="n">lines</span><span class="p">,</span> <span class="n">stable_entries</span><span class="p">,</span> <span class="n">unstable_entries</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">newlines</span> <span class="o">=</span> <span class="p">[[</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="mf">1.0</span> <span class="o">-</span> <span class="n">x</span><span class="p">),</span> <span class="n">y</span><span class="p">]</span> <span class="k">for</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="ow">in</span> <span class="n">lines</span><span class="p">]</span>
            <span class="n">newstable_entries</span> <span class="o">=</span> <span class="p">{(</span><span class="mf">1.0</span> <span class="o">-</span> <span class="n">c</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">c</span><span class="p">[</span><span class="mi">1</span><span class="p">]):</span> <span class="n">entry</span>
                                 <span class="k">for</span> <span class="n">c</span><span class="p">,</span> <span class="n">entry</span> <span class="ow">in</span> <span class="n">stable_entries</span><span class="o">.</span><span class="n">items</span><span class="p">()}</span>
            <span class="n">newunstable_entries</span> <span class="o">=</span> <span class="p">{</span><span class="n">entry</span><span class="p">:</span> <span class="p">(</span><span class="mf">1.0</span> <span class="o">-</span> <span class="n">c</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">c</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
                                   <span class="k">for</span> <span class="n">entry</span><span class="p">,</span> <span class="n">c</span> <span class="ow">in</span>
                                   <span class="n">unstable_entries</span><span class="o">.</span><span class="n">items</span><span class="p">()}</span>
            <span class="k">return</span> <span class="n">newlines</span><span class="p">,</span> <span class="n">newstable_entries</span><span class="p">,</span> <span class="n">newunstable_entries</span>
    <span class="k">elif</span> <span class="n">nameup</span> <span class="o">==</span> <span class="n">ordering</span><span class="p">[</span><span class="mi">1</span><span class="p">]:</span>
        <span class="k">if</span> <span class="n">nameleft</span> <span class="o">==</span> <span class="n">ordering</span><span class="p">[</span><span class="mi">2</span><span class="p">]:</span>
            <span class="n">c120</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">cos</span><span class="p">(</span><span class="mf">2.0</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span> <span class="o">/</span> <span class="mf">3.0</span><span class="p">)</span>
            <span class="n">s120</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sin</span><span class="p">(</span><span class="mf">2.0</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span> <span class="o">/</span> <span class="mf">3.0</span><span class="p">)</span>
            <span class="n">newlines</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="ow">in</span> <span class="n">lines</span><span class="p">:</span>
                <span class="n">newx</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros_like</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
                <span class="n">newy</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros_like</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
                <span class="k">for</span> <span class="n">ii</span><span class="p">,</span> <span class="n">xx</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
                    <span class="n">newx</span><span class="p">[</span><span class="n">ii</span><span class="p">]</span> <span class="o">=</span> <span class="n">c120</span> <span class="o">*</span> <span class="p">(</span><span class="n">xx</span> <span class="o">-</span> <span class="n">cc</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">-</span> <span class="n">s120</span> <span class="o">*</span> <span class="p">(</span><span class="n">y</span><span class="p">[</span><span class="n">ii</span><span class="p">]</span> <span class="o">-</span> <span class="n">cc</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span> <span class="o">+</span> <span class="n">cc</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
                    <span class="n">newy</span><span class="p">[</span><span class="n">ii</span><span class="p">]</span> <span class="o">=</span> <span class="n">s120</span> <span class="o">*</span> <span class="p">(</span><span class="n">xx</span> <span class="o">-</span> <span class="n">cc</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">+</span> <span class="n">c120</span> <span class="o">*</span> <span class="p">(</span><span class="n">y</span><span class="p">[</span><span class="n">ii</span><span class="p">]</span> <span class="o">-</span> <span class="n">cc</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span> <span class="o">+</span> <span class="n">cc</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
                <span class="n">newlines</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="n">newx</span><span class="p">,</span> <span class="n">newy</span><span class="p">])</span>
            <span class="n">newstable_entries</span> <span class="o">=</span> <span class="p">{</span>
                <span class="p">(</span><span class="n">c120</span> <span class="o">*</span> <span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="n">cc</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">-</span> <span class="n">s120</span> <span class="o">*</span> <span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">cc</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span> <span class="o">+</span> <span class="n">cc</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span>
                 <span class="n">s120</span> <span class="o">*</span> <span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="n">cc</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">+</span> <span class="n">c120</span> <span class="o">*</span> <span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">cc</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span> <span class="o">+</span> <span class="n">cc</span><span class="p">[</span><span class="mi">1</span><span class="p">]):</span> <span class="n">entry</span>
                <span class="k">for</span> <span class="n">c</span><span class="p">,</span> <span class="n">entry</span> <span class="ow">in</span> <span class="n">stable_entries</span><span class="o">.</span><span class="n">items</span><span class="p">()}</span>
            <span class="n">newunstable_entries</span> <span class="o">=</span> <span class="p">{</span>
                <span class="n">entry</span><span class="p">:</span> <span class="p">(</span><span class="n">c120</span> <span class="o">*</span> <span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="n">cc</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">-</span> <span class="n">s120</span> <span class="o">*</span> <span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">cc</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span> <span class="o">+</span> <span class="n">cc</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span>
                        <span class="n">s120</span> <span class="o">*</span> <span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="n">cc</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">+</span> <span class="n">c120</span> <span class="o">*</span> <span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">cc</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span> <span class="o">+</span> <span class="n">cc</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
                <span class="k">for</span> <span class="n">entry</span><span class="p">,</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">unstable_entries</span><span class="o">.</span><span class="n">items</span><span class="p">()}</span>
            <span class="k">return</span> <span class="n">newlines</span><span class="p">,</span> <span class="n">newstable_entries</span><span class="p">,</span> <span class="n">newunstable_entries</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">c120</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">cos</span><span class="p">(</span><span class="mf">2.0</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span> <span class="o">/</span> <span class="mf">3.0</span><span class="p">)</span>
            <span class="n">s120</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sin</span><span class="p">(</span><span class="mf">2.0</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span> <span class="o">/</span> <span class="mf">3.0</span><span class="p">)</span>
            <span class="n">newlines</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="ow">in</span> <span class="n">lines</span><span class="p">:</span>
                <span class="n">newx</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros_like</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
                <span class="n">newy</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros_like</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
                <span class="k">for</span> <span class="n">ii</span><span class="p">,</span> <span class="n">xx</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
                    <span class="n">newx</span><span class="p">[</span><span class="n">ii</span><span class="p">]</span> <span class="o">=</span> <span class="o">-</span><span class="n">c120</span> <span class="o">*</span> <span class="p">(</span><span class="n">xx</span> <span class="o">-</span> <span class="mf">1.0</span><span class="p">)</span> <span class="o">-</span> <span class="n">s120</span> <span class="o">*</span> <span class="n">y</span><span class="p">[</span><span class="n">ii</span><span class="p">]</span> <span class="o">+</span> <span class="mf">1.0</span>
                    <span class="n">newy</span><span class="p">[</span><span class="n">ii</span><span class="p">]</span> <span class="o">=</span> <span class="o">-</span><span class="n">s120</span> <span class="o">*</span> <span class="p">(</span><span class="n">xx</span> <span class="o">-</span> <span class="mf">1.0</span><span class="p">)</span> <span class="o">+</span> <span class="n">c120</span> <span class="o">*</span> <span class="n">y</span><span class="p">[</span><span class="n">ii</span><span class="p">]</span>
                <span class="n">newlines</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="n">newx</span><span class="p">,</span> <span class="n">newy</span><span class="p">])</span>
            <span class="n">newstable_entries</span> <span class="o">=</span> <span class="p">{(</span><span class="o">-</span><span class="n">c120</span> <span class="o">*</span> <span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="mf">1.0</span><span class="p">)</span> <span class="o">-</span> <span class="n">s120</span> <span class="o">*</span> <span class="n">c</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="mf">1.0</span><span class="p">,</span>
                                  <span class="o">-</span><span class="n">s120</span> <span class="o">*</span> <span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="mf">1.0</span><span class="p">)</span> <span class="o">+</span> <span class="n">c120</span> <span class="o">*</span> <span class="n">c</span><span class="p">[</span><span class="mi">1</span><span class="p">]):</span> <span class="n">entry</span>
                                 <span class="k">for</span> <span class="n">c</span><span class="p">,</span> <span class="n">entry</span> <span class="ow">in</span> <span class="n">stable_entries</span><span class="o">.</span><span class="n">items</span><span class="p">()}</span>
            <span class="n">newunstable_entries</span> <span class="o">=</span> <span class="p">{</span>
                <span class="n">entry</span><span class="p">:</span> <span class="p">(</span><span class="o">-</span><span class="n">c120</span> <span class="o">*</span> <span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="mf">1.0</span><span class="p">)</span> <span class="o">-</span> <span class="n">s120</span> <span class="o">*</span> <span class="n">c</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="mf">1.0</span><span class="p">,</span>
                        <span class="o">-</span><span class="n">s120</span> <span class="o">*</span> <span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="mf">1.0</span><span class="p">)</span> <span class="o">+</span> <span class="n">c120</span> <span class="o">*</span> <span class="n">c</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
                <span class="k">for</span> <span class="n">entry</span><span class="p">,</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">unstable_entries</span><span class="o">.</span><span class="n">items</span><span class="p">()}</span>
            <span class="k">return</span> <span class="n">newlines</span><span class="p">,</span> <span class="n">newstable_entries</span><span class="p">,</span> <span class="n">newunstable_entries</span>
    <span class="k">elif</span> <span class="n">nameup</span> <span class="o">==</span> <span class="n">ordering</span><span class="p">[</span><span class="mi">2</span><span class="p">]:</span>
        <span class="k">if</span> <span class="n">nameleft</span> <span class="o">==</span> <span class="n">ordering</span><span class="p">[</span><span class="mi">0</span><span class="p">]:</span>
            <span class="n">c240</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">cos</span><span class="p">(</span><span class="mf">4.0</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span> <span class="o">/</span> <span class="mf">3.0</span><span class="p">)</span>
            <span class="n">s240</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sin</span><span class="p">(</span><span class="mf">4.0</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span> <span class="o">/</span> <span class="mf">3.0</span><span class="p">)</span>
            <span class="n">newlines</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="ow">in</span> <span class="n">lines</span><span class="p">:</span>
                <span class="n">newx</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros_like</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
                <span class="n">newy</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros_like</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
                <span class="k">for</span> <span class="n">ii</span><span class="p">,</span> <span class="n">xx</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
                    <span class="n">newx</span><span class="p">[</span><span class="n">ii</span><span class="p">]</span> <span class="o">=</span> <span class="n">c240</span> <span class="o">*</span> <span class="p">(</span><span class="n">xx</span> <span class="o">-</span> <span class="n">cc</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">-</span> <span class="n">s240</span> <span class="o">*</span> <span class="p">(</span><span class="n">y</span><span class="p">[</span><span class="n">ii</span><span class="p">]</span> <span class="o">-</span> <span class="n">cc</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span> <span class="o">+</span> <span class="n">cc</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
                    <span class="n">newy</span><span class="p">[</span><span class="n">ii</span><span class="p">]</span> <span class="o">=</span> <span class="n">s240</span> <span class="o">*</span> <span class="p">(</span><span class="n">xx</span> <span class="o">-</span> <span class="n">cc</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">+</span> <span class="n">c240</span> <span class="o">*</span> <span class="p">(</span><span class="n">y</span><span class="p">[</span><span class="n">ii</span><span class="p">]</span> <span class="o">-</span> <span class="n">cc</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span> <span class="o">+</span> <span class="n">cc</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
                <span class="n">newlines</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="n">newx</span><span class="p">,</span> <span class="n">newy</span><span class="p">])</span>
            <span class="n">newstable_entries</span> <span class="o">=</span> <span class="p">{</span>
                <span class="p">(</span><span class="n">c240</span> <span class="o">*</span> <span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="n">cc</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">-</span> <span class="n">s240</span> <span class="o">*</span> <span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">cc</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span> <span class="o">+</span> <span class="n">cc</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span>
                 <span class="n">s240</span> <span class="o">*</span> <span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="n">cc</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">+</span> <span class="n">c240</span> <span class="o">*</span> <span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">cc</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span> <span class="o">+</span> <span class="n">cc</span><span class="p">[</span><span class="mi">1</span><span class="p">]):</span> <span class="n">entry</span>
                <span class="k">for</span> <span class="n">c</span><span class="p">,</span> <span class="n">entry</span> <span class="ow">in</span> <span class="n">stable_entries</span><span class="o">.</span><span class="n">items</span><span class="p">()}</span>
            <span class="n">newunstable_entries</span> <span class="o">=</span> <span class="p">{</span>
                <span class="n">entry</span><span class="p">:</span> <span class="p">(</span><span class="n">c240</span> <span class="o">*</span> <span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="n">cc</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">-</span> <span class="n">s240</span> <span class="o">*</span> <span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">cc</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span> <span class="o">+</span> <span class="n">cc</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span>
                        <span class="n">s240</span> <span class="o">*</span> <span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="n">cc</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">+</span> <span class="n">c240</span> <span class="o">*</span> <span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">cc</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span> <span class="o">+</span> <span class="n">cc</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
                <span class="k">for</span> <span class="n">entry</span><span class="p">,</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">unstable_entries</span><span class="o">.</span><span class="n">items</span><span class="p">()}</span>
            <span class="k">return</span> <span class="n">newlines</span><span class="p">,</span> <span class="n">newstable_entries</span><span class="p">,</span> <span class="n">newunstable_entries</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">c240</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">cos</span><span class="p">(</span><span class="mf">4.0</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span> <span class="o">/</span> <span class="mf">3.0</span><span class="p">)</span>
            <span class="n">s240</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sin</span><span class="p">(</span><span class="mf">4.0</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span> <span class="o">/</span> <span class="mf">3.0</span><span class="p">)</span>
            <span class="n">newlines</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="ow">in</span> <span class="n">lines</span><span class="p">:</span>
                <span class="n">newx</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros_like</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
                <span class="n">newy</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros_like</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
                <span class="k">for</span> <span class="n">ii</span><span class="p">,</span> <span class="n">xx</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
                    <span class="n">newx</span><span class="p">[</span><span class="n">ii</span><span class="p">]</span> <span class="o">=</span> <span class="o">-</span><span class="n">c240</span> <span class="o">*</span> <span class="n">xx</span> <span class="o">-</span> <span class="n">s240</span> <span class="o">*</span> <span class="n">y</span><span class="p">[</span><span class="n">ii</span><span class="p">]</span>
                    <span class="n">newy</span><span class="p">[</span><span class="n">ii</span><span class="p">]</span> <span class="o">=</span> <span class="o">-</span><span class="n">s240</span> <span class="o">*</span> <span class="n">xx</span> <span class="o">+</span> <span class="n">c240</span> <span class="o">*</span> <span class="n">y</span><span class="p">[</span><span class="n">ii</span><span class="p">]</span>
                <span class="n">newlines</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="n">newx</span><span class="p">,</span> <span class="n">newy</span><span class="p">])</span>
            <span class="n">newstable_entries</span> <span class="o">=</span> <span class="p">{(</span><span class="o">-</span><span class="n">c240</span> <span class="o">*</span> <span class="n">c</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="n">s240</span> <span class="o">*</span> <span class="n">c</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span>
                                  <span class="o">-</span><span class="n">s240</span> <span class="o">*</span> <span class="n">c</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="n">c240</span> <span class="o">*</span> <span class="n">c</span><span class="p">[</span><span class="mi">1</span><span class="p">]):</span> <span class="n">entry</span>
                                 <span class="k">for</span> <span class="n">c</span><span class="p">,</span> <span class="n">entry</span> <span class="ow">in</span> <span class="n">stable_entries</span><span class="o">.</span><span class="n">items</span><span class="p">()}</span>
            <span class="n">newunstable_entries</span> <span class="o">=</span> <span class="p">{</span><span class="n">entry</span><span class="p">:</span> <span class="p">(</span><span class="o">-</span><span class="n">c240</span> <span class="o">*</span> <span class="n">c</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="n">s240</span> <span class="o">*</span> <span class="n">c</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span>
                                           <span class="o">-</span><span class="n">s240</span> <span class="o">*</span> <span class="n">c</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="n">c240</span> <span class="o">*</span> <span class="n">c</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
                                   <span class="k">for</span> <span class="n">entry</span><span class="p">,</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">unstable_entries</span><span class="o">.</span><span class="n">items</span><span class="p">()}</span>
            <span class="k">return</span> <span class="n">newlines</span><span class="p">,</span> <span class="n">newstable_entries</span><span class="p">,</span> <span class="n">newunstable_entries</span></div>
</pre></div>

            <div class="clearer"></div>
          </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">pymatgen 2020.7.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../../pymatgen.html" >pymatgen</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">pymatgen.analysis.phase_diagram</a></li> 
      </ul>
    </div>

    <div class="footer" role="contentinfo">
        &#169; Copyright 2011, Pymatgen Development Team.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 3.1.2.
    </div>
<div class="footer">This page uses <a href="http://analytics.google.com/">
Google Analytics</a> to collect statistics. You can disable it by blocking
the JavaScript coming from www.google-analytics.com.
<script type="text/javascript">
  (function() {
    var ga = document.createElement('script');
    ga.src = ('https:' == document.location.protocol ?
              'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    ga.setAttribute('async', 'true');
    document.documentElement.firstChild.appendChild(ga);
  })();
</script>
</div>

  </body>
</html>